An implementation may choose to expose both virtual key codes and character codes in the keyCode property ('conflated model'), or report separate keyCode and charCode properties ('split model').
The keyCode for keydown / keyup events is calculated as follows:
The keyCode for keypress events is calculated as follows:
The virtual key codes for the following keys do not usually change with keyboard layouts on desktop systems:
The key | virtual key code |
---|---|
Backspace | 8 |
Tab | 9 |
Enter | 13 |
Shift | 16 |
Control | 17 |
Alt | 18 |
Caps Lock | 20 |
Escape | 27 |
Space | 32 |
Page up | 33 |
Page down | 34 |
End | 35 |
Home | 36 |
Left | 37 |
Up | 38 |
Right | 39 |
Down | 40 |
Delete | 46 |
This section is informative
The following punctuation characters may change virtual codes between keyboard layouts, but reporting these values will likely be more compatible with legacy content expecting US-English keyboard layout:
The key | virtual key code |
---|---|
Semicolon (;) | 186 |
Colon (:) | 186 |
Plus (+) | 187 |
Equals sign (=) | 187 |
Comma (,) | 188 |
Less than sign (<) | 188 |
Minus (-) | 189 |
Underscore (_) | 189 |
Period (.) | 190 |
Greater than sign (>) | 190 |
Question mark (?) | 191 |
Forward slash (/) | 191 |
Backtick (`) | 192 |
Tilde (~) | 192 |
Opening square bracket ([) | 219 |
Opening curly bracket ({) | 219 |
Backslash (\) | 220 |
Pipe (|) | 220 |
Closing square bracket (]) | 221 |
Closing curly bracket (}) | 221 |
Single quote (') | 222 |
Double quote (") | 222 |
Thanks to Anne van Kesteren, Alexey Proskuryakov, Olli Pettay, Bjoern Hoehrmann and Oliver Hunt for comments on earlier (and more complicated) versions of this draft.