Hello reader's! If you are making the website based on user press key events. Then to detect the arrow key pressed by user we can code as below:-
document.onkeydown = function(e) {
switch (e.keyCode) {
case 37:
alert('You just press Left key');//Left arrow key is pressed
break;
case 38:
alert('You just press Up key');//Up arrow key is pressed
break;
case 39:
alert('You just press Right key'); //right arrow key is pressed
break;
case 40:
alert('You just press Down key'); //Down arrow key is pressed
break;
}
};
The output will be an alert box saying 'Left arrow key is pressed'
0 Comment(s)