- KeyboardEvent: key property
- Value
- KeyboardEvent sequence
- KeyboardEvent sequence example
- HTML
- CSS
- JavaScript
- Result
- Case 1
- Case 2
- Examples
- Specifications
- Browser compatibility
- Found a content problem with this page?
- Element: keydown event
- Syntax
- Event type
- Event properties
- Examples
- addEventListener keydown example
- onkeydown equivalent
KeyboardEvent: key property
The KeyboardEvent interface’s key read-only property returns the value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.
Value
Its value is determined as follows:
- 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 «.
- 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 .
KeyboardEvent sequence
Every KeyboardEvent is fired in a pre-determined sequence. For a given key press, the sequence of KeyboardEvent s fired is as follows assuming that Event.preventDefault is not called:
- A keydown event is first fired. If the key is held down further and the key produces a character key, then the event continues to be emitted in a platform implementation dependent interval and the KeyboardEvent.repeat read only property is set to true .
- If the key produces a character key that would result in a character being inserted into possibly an , or an element with HTMLElement.contentEditable set to true, the beforeinput and input event types are fired in that order. Note that some other implementations may fire keypress event if supported. The events will be fired repeatedly while the key is held down.
- A keyup event is fired once the key is released. This completes the process.
In sequence 1 & 3, the KeyboardEvent.key attribute is defined and is set appropriately to a value according to the rules defined earlier.
KeyboardEvent sequence example
Consider the event sequence generated when we interact with the Shift and the 2 key using a U.S keyboard layout as compared to when we do so using a UK keyboard layout.
Try experimenting using the following two test cases:
- Press and hold the Shift key, then press 2 and release it. Next, release the Shift key.
- Press and hold the Shift key, then press and hold 2 . Release the Shift key. Finally, release 2 .
HTML
div class="fx"> div> textarea rows="5" name="test-target" id="test-target">textarea> button type="button" name="btn-reset" id="btn-reset">Resetbutton> div> div class="flex"> pre id="console-log">pre> div> div>
CSS
.fx -webkit-display: flex; display: flex; margin-left: -20px; margin-right: -20px; > .fx > div padding-left: 20px; padding-right: 20px; > .fx > div:first-child width: 30%; > .flex -webkit-flex: 1; flex: 1; > #test-target display: block; width: 100%; margin-bottom: 10px; >
JavaScript
let textarea = document.getElementById("test-target"), consoleLog = document.getElementById("console-log"), btnReset = document.getElementById("btn-reset"); function logMessage(message) consoleLog.innerHTML += `$message> `; > textarea.addEventListener("keydown", (e) => if (!e.repeat) logMessage(`Key "$e.key>" pressed [event: keydown]`); > else logMessage(`Key "$e.key>" repeating [event: keydown]`); > >); textarea.addEventListener("beforeinput", (e) => logMessage(`Key "$e.data>" about to be input [event: beforeinput]`); >); textarea.addEventListener("input", (e) => logMessage(`Key "$e.data>" input [event: input]`); >); textarea.addEventListener("keyup", (e) => logMessage(`Key "$e.key>" released [event: keyup]`); >); btnReset.addEventListener("click", (e) => let child = consoleLog.firstChild; while (child) consoleLog.removeChild(child); child = consoleLog.firstChild; > textarea.value = ""; >);
Result
Note: On browsers that don’t fully implement the InputEvent interface which is used for the beforeinput and input events, you may get incorrect output on those lines of the log output.
Case 1
When the shift key is pressed, a keydown event is first fired, and the key property value is set to the string Shift . As we keep holding this key, the keydown event does not continue to fire repeatedly because it does not produce a character key.
When key 2 is pressed, another keydown event is fired for this new key press, and the key property value for the event is set to the string @ for the U.S keyboard type and » for the UK keyboard type, because of the active modifier shift key. The beforeinput and input events are fired next because a character key has been produced.
As we release the key 2 , a keyup event is fired and the key property will maintain the string values @ and » for the different keyboard layouts respectively.
As we finally release the shift key, another keyup event is fired for it, and the key attribute value remains Shift .
Case 2
When the shift key is pressed, a keydown event is first fired, and the key property value is set to be the string Shift . As we keep holding this key, the keydown event does not continue to fire repeatedly because it produced no character key.
When key 2 is pressed, another keydown event is fired for this new key press, and the key property value for the event is set to be the string @ for the U.S keyboard type and » for the UK keyboard type, because of the active modifier shift key. The beforeinput and input events are fired next because a character key has been produced. As we keep holding the key, the keydown event continues to fire repeatedly and the KeyboardEvent.repeat property is set to true . The beforeinput and input events are fired repeatedly as well.
As we release the shift key, a keyup event is fired for it, and the key attribute value remains Shift . At this point, notice that the key property value for the repeating keydown event of the key 2 key press is now «2» because the modifier shift key is no longer active. The same goes for the InputEvent.data property of the beforeinput and input events.
As we finally release the key 2 , a keyup event is fired but the key property will be set to the string value 2 for both keyboard layouts because the modifier shift key is no longer active.
Examples
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).
.addEventListener( "keydown", (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, );
Specifications
Browser compatibility
BCD tables only load in the browser
Found a content problem with this page?
Element: keydown event
The keydown event is fired when a key is pressed.
Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value.
The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase «a» will be reported as 65 by keydown and keyup , but as 97 by keypress . An uppercase «A» is reported as 65 by all events.
Keyboard events are only generated by , , and anything with the contentEditable or tabindex attribute. If not caught, they bubble up the DOM tree until they reach Document .
Since Firefox 65, the keydown and keyup events are now fired during IME composition, to improve cross-browser compatibility for CJKT users (Firefox bug 354358). To ignore all keydown events that are part of composition, do something like this (229 is a special value set for a keyCode relating to an event that has been processed by an IME):
.addEventListener("keydown", (event) => if (event.isComposing || event.keyCode === 229) return; > // do something >);
Syntax
Use the event name in methods like addEventListener() , or set an event handler property.
addEventListener("keydown", (event) => >); onkeydown = (event) => >;
Event type
Event properties
This interface also inherits properties of its parents, UIEvent and Event . KeyboardEvent.altKey Read only Returns a boolean value that is true if the Alt ( Option or ⌥ on macOS) key was active when the key event was generated. KeyboardEvent.code Read only Returns a string with the code value of the physical key represented by the event.
Warning: This ignores the user’s keyboard layout, so that if the user presses the key at the «Y» position in a QWERTY keyboard layout (near the middle of the row above the home row), this will always return «KeyY», even if the user has a QWERTZ keyboard (which would mean the user expects a «Z» and all the other properties would indicate a «Z») or a Dvorak keyboard layout (where the user would expect an «F»). If you want to display the correct keystrokes to the user, you can use Keyboard.getLayoutMap() .
KeyboardEvent.ctrlKey Read only Returns a boolean value that is true if the Ctrl key was active when the key event was generated. KeyboardEvent.isComposing Read only Returns a boolean value that is true if the event is fired between after compositionstart and before compositionend . KeyboardEvent.key Read only Returns a string representing the key value of the key represented by the event. KeyboardEvent.locale Read only Returns a string representing a locale string indicating the locale the keyboard is configured for. This may be the empty string if the browser or device doesn’t know the keyboard’s locale.
Note: This does not describe the locale of the data being entered. A user may be using one keyboard layout while typing text in a different language.
KeyboardEvent.location Read only Returns a number representing the location of the key on the keyboard or other input device. A list of the constants identifying the locations is shown in Keyboard locations. KeyboardEvent.metaKey Read only Returns a boolean value that is true if the Meta key (on Mac keyboards, the ⌘ Command key; on Windows keyboards, the Windows key ( ⊞ )) was active when the key event was generated. KeyboardEvent.repeat Read only Returns a boolean value that is true if the key is being held down such that it is automatically repeating. KeyboardEvent.shiftKey Read only Returns a boolean value that is true if the Shift key was active when the key event was generated.
Examples
addEventListener keydown example
input placeholder="Click here, then press down a key." size="40" /> p id="log">p>
const input = document.querySelector("input"); const log = document.getElementById("log"); input.addEventListener("keydown", logKey); function logKey(e) log.textContent += ` $e.code>`; >