KeyboardEvent.key

The KeyboardEvent.key read-only property returns the value of the key pressed by the user while taking into considerations the state of modifier keys such as the shiftKey as well as the keyboard locale/layout. 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

KeyboardEvents are fired in a pre-determined sequence and understanding this will go a long way into understanding the key property value for a particular KeyboardEvent. For a given key press, the sequence of KeyboardEvents 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 <input>, <textarea> 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 ealier.

KeyboardEvent Sequence Sample

Consider the event sequence generated when we interact with the ShiftKey and the legend key 2 using a U.S keyboard layout and a UK keyboard layout.

Result

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.

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).