js on key press arrow code example
Example 1: javascript detect arrow key press
document.addEventListener('keydown', 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;
}
});
Example 2: arrow keys function javascript
document.addEventListener('keydown', 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;
}
});
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '38') {
}
else if (e.keyCode == '40') {
}
else if (e.keyCode == '37') {
}
else if (e.keyCode == '39') {
}
}