Created
April 22, 2019 13:09
-
-
Save akhileshdarjee/0a47dfdf8f8e1813dcb511a5c5af2aaf to your computer and use it in GitHub Desktop.
Binding Direction Arrow Keys Up, Down, Left and Right in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.onkeydown = function(e) { | |
switch(e.which) { | |
case 37: | |
// left direction | |
break; | |
case 38: | |
// up direction | |
break; | |
case 39: | |
// right direction | |
break; | |
case 40: | |
// down direction | |
break; | |
default: return; // exit this handler for other keys | |
} | |
e.preventDefault(); // prevent the default action (scroll / move caret) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment