Skip to content
Open navbar menu
Sign UpLog In
Profile icon

Joshua Rogers

@JoshuaRogers6
JoshuaRogers6
JoshuaRogers6
shared a Post
2 years ago
How do I detect if an arrow key is pressed in JS?
How do I detect if an arrow key is pressed in JS? I am using HTML, and JavaScript canvas on a project and need to detect arrow keys pressed.
Bookie0
Bookie0
Check this out: The arrow keys have key codes, so for example the left arrow key has the code 37, right arrow key code is 39, up is 38, and down is 40. Then you could use switch cases to determine which key was pressed: document.onkeydown = function (event) { // to get the key switch (event.keyCode) { case 37: // if it's the left arrow console.log("Left key is pressed."); break; case 38: // if it's the up arrow console.log("Up key is2 years ago
JoshuaRogers6
JoshuaRogers6
Thank you!2 years ago