Reference keyCode

keyCode

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

Notice any errors or typos? Please let us know. Please feel free to edit src/events/keyboard.js and open a pull request!

Related References