0

I need to detect arrow key input in javascript. Currently I am doing this:

document.onkeydown = function(event) {
    if (event.which == 37) {
        ...
    } else if (event.which == 39) {
        ...
    } else if (event.which == 38) {
        ...
    } else if (event.which == 40) {
        ...
    }
};

document.onkeyup = function(event) {
    if (event.which == 37) {
        ... 
    } else if (event.which == 39) {
        ...
    } else if (event.which == 38) {
        ...
    } else if (event.which == 40) {
        ...
    } 
};

This works fine, but when i press command + right and release the arrow key first, I get the down event for the arrow key, but only get an up event when releasing the command key. Also the keycode is 91 regardless of which arrow key I was pressing. How do I handle this situation?

I am on a mac with google chrome.

zumba man
  • 129
  • 1
  • 12
  • what is cmd? ... – otto Mar 30 '18 at 00:05
  • the command key on mac – zumba man Mar 30 '18 at 00:24
  • `event.keyCode` / `event.code`? – Meghan Mar 30 '18 at 00:28
  • `event.keyCode` is 91 `event.code` is "MetaLeft" – zumba man Mar 30 '18 at 03:05
  • Possible duplicate of [Missing keyUp events on meaningful key combinations (e.g. "Select till beginning of line")](https://stackoverflow.com/questions/4001565/missing-keyup-events-on-meaningful-key-combinations-e-g-select-till-beginning) – zumba man Mar 30 '18 at 22:04
  • It seems like this is the expected behavior on OSX. [Described here.](https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) Not sure how to handle this. – zumba man Mar 30 '18 at 22:49

0 Answers0