Using keyboard arrows as an option

this is my site,


Is there a way to use the arrow keys on the keyboard for the lazy user to b e an option instead of clicking those arrows?

Here is a bit of JS I have used to detect key press actions, which I’m sure you could adapt to your situation.

document.addEventListener("keyup", function(e) {
  if (e.ctrlKey && e.key == "b")
    btn[0].click();
  else if (e.ctrlKey && e.key == "i")
    btn[1].click();
  else if (e.altKey && e.key == "3")
    btn[3].click();
  else if (e.altKey && e.key == "4")
    btn[4].click();
  else if (e.altKey && e.key == "5")
    btn[5].click();
});

Update: for arrow keys you’ll need e.key == "ArrowLeft" etc

I would take issue with this. Some people find it difficult to use a mouse or simply prefer to use the keyboard. There’s nothing lazy in that.

3 Likes

I would not override the arrow keys as these are typically expected to be used for scrolling, selection and the like… so IMHO this would actually impair accessibility rather than improve it. At least require the alt key to be pressed simultaneously or something like that (edit: ah, just as implemented by @Gandalf). :-)

Another simple option would be adding accessKey attributes… although this won’t work with arrow keys AFAIK, you’d have to use WASD instead or the number pad.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.