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.
KeyboardEvent
represents the press of a dead key, the key value must be "Dead
".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."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.
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).
window.addEventListener("keydown", function (event) { if (event.defaultPrevented) { return; // Do nothing if the event was already processed } 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. } // Cancel the default action to avoid it being handled twice event.preventDefault(); }, true);
Specification | Status | Comment |
---|---|---|
Document Object Model (DOM) Level 3 Events Specification The definition of 'KeyboardEvent.key' in that specification. | Obsolete | Initial definition, included key values. |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 51.0 | (Yes) | 23.0 (23.0) | 9.0[1] | 38.0 | (Yes) |
non-printable keys | 51.0 | ? | 23.0 (23.0) | 9.0[1] | 38.0 | ? |
printable keys | 51.0 | ? | 29.0 (29.0) | 9.0[1] | 38.0 | ? |
dead key | 51.0 | ? | No support | No support | 38.0 | ? |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | No support | 51.0 | (Yes) | 23.0 (23.0) | ? | 38.0 | (Yes) | 51.0 |
non-printable keys of virtual keyboard | No support | 51.0 | ? | 23.0 (23.0) | ? | 38.0 | ? | 51.0 |
printable keys of virtual keyboard | No support | 51.0 | ? | ? | ? | 38.0 | ? | 51.0 |
non-printable keys of physical keyboard | No support | 51.0 | ? | 23.0 (23.0) | ? | 38.0 | ? | 51.0 |
printable keys of physical keyboard | No support | 51.0 | ? | 29.0 (29.0) | ? | 38.0 | ? | 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.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key