Created
February 6, 2020 02:53
-
-
Save royce002/4dec4bd0849b907a72d71aa8ecd0e8e3 to your computer and use it in GitHub Desktop.
This little function detects the arrow keys on a keyboard. 2 Functions , one in an if else and the other in a switch case.
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 = checkKey; | |
function checkKey(e) { | |
e = e || window.event; | |
if (e.keyCode == '38') { | |
// up arrow | |
} | |
else if (e.keyCode == '40') { | |
// down arrow | |
} | |
else if (e.keyCode == '37') { | |
// left arrow | |
} | |
else if (e.keyCode == '39') { | |
// right arrow | |
} | |
} | |
/*************************/ | |
document.onkeydown = function(e) { | |
switch (e.keyCode) { | |
case 37: | |
alert('left'); | |
break; | |
case 38: | |
alert('up'); | |
break; | |
case 39: | |
alert('right'); | |
break; | |
case 40: | |
alert('down'); | |
break; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment