Get the keyboard button that was pressed when a key event occured:
The result of x could be:
More "Try it Yourself" examples below.
The key property returns the identifier of the key that was pressed when a key event occured.
Key identifiers are strings that identify keyboard buttons. The return value of this property can be a string of:
Note: This property is read-only.
Tip: If you want to find out whether the "ALT", "CTRL", "META" or "SHIFT" key was pressed when a key event occured, use the altKey, ctrlKey, metaKey or shiftKey property.
The numbers in the table specify the first browser version that fully supports the property.
Property | |||||
---|---|---|---|---|---|
key | Not supported | 9.0 | 23.0 | Not supported | Not supported |
Return Value: |
A String, representing the pressed keyboard button. Possible values:
|
---|---|
DOM Version: | DOM Level 3 Events |
Alert some text if the user presses the "A" key:
HTML DOM reference: KeyboardEvent keyCode Property
HTML DOM reference: KeyboardEvent which Property
HTML DOM reference: KeyboardEvent charCode Property