Skip to content

Instantly share code, notes, and snippets.

@akhileshdarjee
Created April 22, 2019 13:09
Show Gist options
  • Save akhileshdarjee/0a47dfdf8f8e1813dcb511a5c5af2aaf to your computer and use it in GitHub Desktop.
Save akhileshdarjee/0a47dfdf8f8e1813dcb511a5c5af2aaf to your computer and use it in GitHub Desktop.
Binding Direction Arrow Keys Up, Down, Left and Right in Javascript
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