0

How can I listen for two keydown events simultaneously, in the most idomatic way possible, without any third party library or framework?

Example code (not working):

myFile.js

handleKeyDown(e) {
    switch (e.keyCode) {
        case 32 && 39: { // SPACE + RightArrow
            e.preventDefault();
            console.log(e)
        }
    }
}
Kin
  • 1,435
  • 10
  • 16

1 Answers1

0

There is not only one event you should listen to. But you should listen first when the space is down with keydown event, and listen when the space is up with keyup event and store that in a global state. Then listen for when right arrow is pressed, test if the space is down using the global state we set previously, and if the space is down when right arrow is pressed, you have your event.

  • I'm not sure how I'd implement this in an efficient way, that's whats causing my question rly –  Aug 13 '19 at 15:52