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});
1//detecting arrow key presses - supports both new and depricated methods
2document.addEventListener('keydown', function(e) {
3 var key = e.key || e.keyCode;
4 switch (key) {
5 case 'ArrowLeft': case 37:
6 alert('left');
7 break;
8 case 'ArrowUp': case 38:
9 alert('up');
10 break;
11 case 'ArrowRight': case 39:
12 alert('right');
13 break;
14 case 'ArrowDown': case 40:
15 alert('down');
16 break;
17 }
18});
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}