1//detecting arrow key presses
2document.addEventListener('keydown', function(e) {
3 switch (e.keyCode) {
4 case 37:
5 alert('left');
6 break;
7 case 38:
8 alert('up');
9 break;
10 case 39:
11 alert('right');
12 break;
13 case 40:
14 alert('down');
15 break;
16 }
17});
1document.onkeydown = checkKey;
2
3function checkKey(e) {
4
5 e = e || window.event;
6
7 if (e.keyCode == '38') {
8 // up arrow
9 }
10 else if (e.keyCode == '40') {
11 // down arrow
12 }
13 else if (e.keyCode == '37') {
14 // left arrow
15 }
16 else if (e.keyCode == '39') {
17 // right arrow
18 }
19
20}