KeyboardEvent.key

Draft
This page is not complete.

The KeyboardEvent.key read-only property returns the value of a key or keys pressed by the user. Its value is determined as follows:

See a full list of key values.

  • If the pressed key has a printed representation, the returned value is a non-empty Unicode character string containing the printable representation of the key.
  • If the pressed key is a control or special character, the returned value is one of the pre-defined key values.
  • If the KeyboardEvent represents the press of a dead key, the key value must be "Dead".
  • If more than one key is pressed and the combination includes a modifier that makes the resulting keystroke non printing, the returned value is the printable character. For example, if the combination were Control + a, the letter "a" is returned.
  • Some specialty keyboard keys (such as the extended keys for controlling media on multimedia keyboards) don't generate key codes on Windows; instead, they trigger WM_APPCOMMAND events. These events get mapped to DOM keyboard events, and are listed among the "Virtual key codes" for Windows, even though they aren't actually key codes.
  • If the key cannot be identified, the returned value is "Unidentified".

This page is undergoing heavy updating to improve its layout, modernize its content, and bring everything up to date. Newer content is nearer the top; to find the older tables, see Previous versions of tables. Once the updates are finished, that content will be removed. No information should be lost once it's all done.

Example

This example uses EventTarget.addEventListener() to listen for keydown events. When they occur,  the key's value is checked to see if it's one of the keys the code is interested in, and if it is, it gets processed in some way (possibly by steering a spacecraft, perhaps by changing the selected cell in a spreadsheet).

JavaScript
window.addEventListener("keydown", function (event) {
  if (event.defaultPrevented) {
    return; // Should do nothing if the key event was already consumed.
  }

  switch (event.key) {
    case "ArrowDown":
      // Do something for "down arrow" key press.
      break;
    case "ArrowUp":
      // Do something for "up arrow" key press.
      break;
    case "ArrowLeft":
      // Do something for "left arrow" key press.
      break;
    case "ArrowRight":
      // Do something for "right arrow" key press.
      break;
    case "Enter":
      // Do something for "enter" or "return" key press.
      break;
    case "Escape":
      // Do something for "esc" key press.
      break;
    default:
      return; // Quit when this doesn't handle the key event.
  }

  // Consume the event to avoid it being handled twice
  event.preventDefault();
}, true);

Specification

Specification Status Comment
Document Object Model (DOM) Level 3 Events Specification
The definition of 'KeyboardEvent.key' in that specification.
Working Draft Initial definition, included key values.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

51.0

23.0 (23.0) 9.0[1] 38.0 No support [2]
non-printable keys 51.0 23.0 (23.0) 9.0[1] 38.0 No support
printable keys 51.0 29.0 (29.0) 9.0[1] 38.0 No support
dead key 51.0 No support No support 38.0 No support
s
Feature Android Android Webview Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support 51.0 23.0 (23.0) ? 38.0 No support [2] 51.0
non-printable keys of virtual keyboard No support 51.0 23.0 (23.0) ? 38.0 No support 51.0
printable keys of virtual keyboard No support 51.0 ? ? 38.0 No support 51.0
non-printable keys of physical keyboard No support 51.0 23.0 (23.0) ? 38.0 No support 51.0
printable keys of physical keyboard No support 51.0 29.0 (29.0) ? 38.0 No support 51.0

[1]: Internet Explorer's implementation does not completely match the current spec because it is based on an older version of the spec.

[2]: WebKit bug #69029

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/keyboardevent/key

API DOM KeyboardEvent Property Read-only Reference