Detect arrow key press
ARROW KEYS :
- Arrow keys consists of group of keys used for navigating .
- arrow keys are used to move the cursor from one line text to another
- there are usually four buttons used in keyboards such as up,down,left,right.
- each arrow keys have a specific key code.
- arrow keys with shift key used for cut and copy .
explanation :
every key on keyboard has a specified meaning and key code. Here we are going to detect a key code on our computer .
Before computer mouse had been widely used moving the cursor on the screen was mostly used with arrow keys.
first of all every keyboard has a four arrow keys such as
- left arrow key
- right arrow key
- up arrow key
- down arrow key
these four arrow keys have a unique key code that can be used by Javascript to detect arrow key press
key codes :
- left arrow key-37
- up arrow key -38
- right arrow key-39
- down arrow key-40
Other learing platforms and tutorials :
How to Detect Arrow Keypress in JavaScript
http://www.codespeedy.comcode to detect arrow key press :
<!DOCTYPE html> <html> <head> <title>detect arrow keys in javascript</title> </head> <body> <script type="text/javascript"> document.onkeydown =function(event) { switch(event.keycode){ case 37 : alert('left key pressed'); break; case 38: alrert('up key pressed'); break; case 39: alrert('right key pressed'); break; case 40: alrert('down key pressed'); break; } } </script> </body> </html>