A Number
system variable that contains the code of the last key pressed.
Every key has a numeric key code. For example, the letter a
key has the key code 65. Use this key code to determine which key was pressed by comparing it to the numeric value of the desired key.
For example, to detect when the Enter key is pressed:
if (keyCode === 13) { // Enter key
// Code to run if the Enter key was pressed.
}
Alternatively, you can use the key function to directly compare the key value:
if (key === 'Enter') { // Enter key
// Code to run if the Enter key was pressed.
}
Use the following numeric codes for the arrow keys:
Up Arrow: 38Down Arrow: 40Left Arrow: 37Right Arrow: 39
More key codes can be found at websites such as keycode.info.
Examples
Related References
key
A String system variable that contains the value of the last key typed.
keyCode
A Number system variable that contains the code of the last key pressed.
keyIsDown
Returns true if the key it’s checking is pressed and false if not.
keyIsPressed
A Boolean system variable that's true if any key is currently pressed and false if not.