JS Keyboard Events
When the user hits the keyboard, the order of events being fired is:
- (on)keydown – may act upon or cancel the keystroke – the only way to catch special keys such as shift, ctrl, del – gets the key-code on event.which or event.keyCode
- (on)keypress – may act upon, cancel or modify (only IE) the keystroke – this event is repeated as long as the key is held down – gets the ASCII-code on event.which or event.keyCode
- (on)textInput – also called on clipboard paste – as of 2010, only supported by webkit browsers
- (on)keyup – may act upon release of the key – too late to do anything about it
Some browsers also support the event.charCode property, which will return the ASCII code of the key in ALL handlers. The DOM level 3 specs introduce the properties event.keyIdentifier, which holds a string with the key´s name, and event.keyLocation, which holds a number for the key´s placement.
Values of event.keyIdentifier
key code | short name | DOM3 name (string) |
| Ac | Alphanumeric |
| Again | Again |
| AlCs | AllCandidates |
18 | Alt | Alt |
93 | Apps | Apps |
30 | At | Accept |
| AtGh | AltGraph |
246 | Attn | Attn |
8 | BkSp | U+000008 |
166 | BrBk | BrowserBack |
167 | BrFd | BrowserForward |
171 | BrFs | BrowserFavorites |
172 | BrHe | BrowserHome |
168 | BrRh | BrowserRefresh |
170 | BrSh | BrowserSearch |
169 | BrSp | BrowserStop |
| Ce | Compose |
| CeIt | CodeInput |
17 | Cl | Control |
12 | Clear | Clear |
24 | Cncl | U+000018 |
| Copy | Copy |
247 | Crsel | Crsel |
20 | CsLk | CapsLock |
28 | Ct | Convert |
| Cut | Cut |
46 | Del | U+00007F |
40 | Down | Down |
43 | Ee | Execute |
249 | EeEf | EraseEof |
35 | End | End |
13 | Enter | Enter |
27 | Esc | U+00001B |
248 | Exsel | Exsel |
112 | F1 | F1 |
121 | F10 | F10 |
122 | F11 | F11 |
123 | F12 | F12 |
124 | F13 | F13 |
125 | F14 | F14 |
126 | F15 | F15 |
127 | F16 | F16 |
128 | F17 | F17 |
129 | F18 | F18 |
130 | F19 | F19 |
113 | F2 | F2 |
131 | F20 | F20 |
132 | F21 | F21 |
133 | F22 | F22 |
134 | F23 | F23 |
135 | F24 | F24 |
114 | F3 | F3 |
115 | F4 | F4 |
116 | F5 | F5 |
117 | F6 | F6 |
118 | F7 | F7 |
119 | F8 | F8 |
120 | F9 | F9 |
| Find | Find |
24 | FlMe | FinalMode |
| FlWh | FullWidth |
| Ha | Hiragana |
| HaMe | HanjaMode |
47 | Help | Help |
| HfWh | HalfWidth |
| HlMe | HangulMode |
36 | Home | Home |
45 | It | Insert |
23 | JaMe | JunjaMode |
| JeHa | JapaneseHiragana |
| JeKa | JapaneseKatakana |
| JeRi | JapaneseRomaji |
| Ka | Katakana |
| KaMe | KanaMode |
| KiMe | KanjiMode |
37 | Left | Left |
181 | LhAn1 | LaunchApplication1 |
182 | LhAn2 | LaunchApplication2 |
180 | LhMl | LaunchMail |
176 | MaNtTk | MediaNextTrack |
177 | MaPsTk | MediaPreviousTrack |
179 | MaPyPe | MediaPlayPause |
178 | MaSp | MediaStop |
31 | MeCe | ModeChange |
224 | Meta | Meta |
29 | Nt | Nonconvert |
144 | NumLk | NumLock |
| Paste | Paste |
19 | Pause | Pause |
34 | PeDn | PageDown |
33 | PeUp | PageUp |
250 | Play | Play |
42 | Print |
| Props | Props |
229 | Ps | Process |
| PsCe | PreviousCandidate |
44 | PtSn | PrintScreen |
39 | Right | Right |
| RnCs | RomanCharacters |
16 | Shift | Shift |
145 | Sl | Scroll |
95 | Sleep |
32 | Space | U+000020 |
41 | St | Select |
| StMa | SelectMedia |
| Stop | Stop |
9 | Tab | U+000009 |
| Undo | Undo |
38 | Up | Up |
174 | VeDn | VolumeDown |
173 | VeMe | VolumeMute |
175 | VeUp | VolumeUp |
91/92 | Win | Win |
251 | Zoom | Zoom |
Values of event.keyIdentifier
value | key location |
0 | default, eg. "A"~"Z", "%" (over 5), left-shift-key |
1 | side, eg. right-ctrl-key |
2 | keypad, eg. "0"~"9" (numeric pad), right-enter-key |