if(e.keyCode == 38) code example
Example 1: javascript detect arrow key press
//detecting arrow key presses
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: js detect right key click
//detecting arrow key presses - supports both new and depricated methods
document.addEventListener('keydown', function(e) {
var key = e.key || e.keyCode;
switch (key) {
case 'ArrowLeft': case 37:
alert('left');
break;
case 'ArrowUp': case 38:
alert('up');
break;
case 'ArrowRight': case 39:
alert('right');
break;
case 'ArrowDown': case 40:
alert('down');
break;
}
});
Example 3: keycode letters
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
a 65
b 66
c 67
d 68
e 69
f 70
g 71
h 72
i 73
j 74
k 75
l 76
m 77
n 78
o 79
p 80
q 81
r 82
s 83
t 84
u 85
v 86
w 87
x 88
y 89
z 90