Reference keyCode

keyCode

A Number system variable that contains the code of the last key typed.

All keys have a keyCode. For example, the a key has the keyCode 65. The keyCode variable is helpful for checking whether a special key has been typed. For example, the following conditional checks whether the enter key has been typed:

if (keyCode === 13) {
  // Code to run if the enter key was pressed.
}

The same code can be written more clearly using the system variable ENTER which has a value of 13:

if (keyCode === ENTER) {
  // Code to run if the enter key was pressed.
}

The system variables BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, and RIGHT_ARROW are all helpful shorthands the key codes of special keys. Key codes can be found on websites such as keycode.info.

Examples


Related References