Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The deprecated KeyboardEvent.keyCode read-only property represents a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key. If the key can't be identified, this value is 0.
You should avoid using this if possible; it's been deprecated for some time. Instead, you should use KeyboardEvent.code, if it's implemented. Unfortunately, some browsers still don't have it, so you'll have to be careful to make sure you use one which is supported on all target browsers.
Note: Web developers shouldn't use the keyCode attribute for printable characters when handling keydown and keyup events. As described above, the keyCode attribute is not useful for printable characters, especially those input with the Shift or Alt key pressed. When implementing a shortcut key handler, the keypress event is usually better (at least when Gecko is the runtime in use).
Examples
js
window.addEventListener("keydown",(event)=>{if(event.defaultPrevented){return;// Should do nothing if the default action has been cancelled}let handled =false;if(event.key !==undefined){// Handle the event with KeyboardEvent.key
handled =true;}elseif(event.keyCode !==undefined){// Handle the event with KeyboardEvent.keyCode
handled =true;}if(handled){// Suppress "double action" if event handled
event.preventDefault();}},true,);
The value of key events which are caused by pressing or releasing printable keys in standard position is not compatible between browsers.
IE just exposes the native virtual keycode value as KeyboardEvent.keyCode.
Google Chrome, Chromium and Safari must decide the value from the input character. If the inputting character can be inputted with the US keyboard layout, they use the keyCode value on the US keyboard layout.
Firefox gets keyCode values from ASCII characters inputtable by the key — even with shift modifiers or an ASCII capable keyboard layout. See the following rules for details:
If the system is Windows and the native keycode of the pressed key indicates that the key is a-z or 0-9, use a keycode for it.
If the system is Mac and the native keycode of the pressed key indicates that the key is 0-9, use a keycode for it.
If the pressed key inputs an ASCII alphabetic or numeric character with no modifier key, use a keycode for it.
If the pressed key inputs an ASCII alphabetic or numeric character with a Shift key modifier, use a keycode for it.
If the pressed key inputs a different ASCII character with no modifier key, use a keycode for it.
If the pressed key inputs a different ASCII character with a Shift key modifier, use a keycode for it.
Otherwise, i.e., pressed key inputs a unicode character:
If the keyboard layout is ASCII-capable (i.e., can input ASCII alphabets), use 0 or compute with the following additional rules.
Otherwise, i.e., the keyboard layout isn't ASCII capable, use the ASCII capable keyboard layout installed on the environment with the highest priority:
If the pressed key on the alternative keyboard layout inputs an ASCII alphabetic or numeric character, use a keycode for it.
Gecko sets keyCode values of punctuation keys as far as possible (when points 7.1 or 7.2 in the above list are reached) with the following rules:
Warning: The purpose of these new additional rules is for making users whose keyboard layouts map unicode characters to punctuation keys in a US keyboard layout can use web applications which support Firefox only with ASCII-capable keyboard layouts or just with a US keyboard layout. Otherwise, the newly mapped keyCode values may be conflict with other keys. For example, if the active keyboard layout is Russian, the keyCode value of both the "Period" key and "Slash" key are 190 (KeyEvent.DOM_VK_PERIOD). If you need to distinguish those keys but you don't want to support all keyboard layouts in the world by yourself, you should probably use KeyboardEvent.code.
If running macOS or Linux:
If the active keyboard layout is not ASCII-capable and an alternative ASCII-capable keyboard layout is available.
If the alternative ASCII-capable keyboard layout produces an ASCII character via just the unmodified key, use a keyCode for the character.
If the alternative ASCII-capable keyboard layout produces an ASCII character with a Shift key modifier, use a keyCode for the shifted character.
Otherwise, use a keyCode for an ASCII character produced by the key when the US keyboard layout is active.
Otherwise, use a keyCode for an ASCII character produced by the key when the US keyboard layout is active.
If running on Windows:
Use a keyCode value for an ASCII character produced by a key which is mapped to the same virtual keycode of Windows when the US keyboard layout is active.
keyCode values of each browser's keydown event caused by printable keys in standard position
[1] The value is input from JIS keyboard. When you use ANSI keyboard, the keyCode value and inputted character are what you select from the US keyboard layout.
[2] The key is a dead key. The value of keyup event is 0xBA (186).
[3] The key is a dead key. The value of keyup event is 0x10 (16).
[4] No key events are fired.
[5] The key isn't available with Greek keyboard layout (does not input any character). The value of keyup event is 0x00 (0).
Non-printable keys (function keys)
keyCode values of each browser's keydown event caused by modifier keys:
[1] On Windows, pressing the AltGraph key raises both the "ControlLeft" and the "AltRight" key events.
[2] When the Japanese keyboard layout is active, pressing the CapsLock key without pressing Shift raises 0xF0 (240). The key works as the Alphanumeric key whose label is "英数".
[3] When the Japanese keyboard layout is active, pressing the "CapsLock" key without pressing Shift raises 0x00 (0). The key works as the "Alphanumeric" key whose label is "英数".
keyCode values of each browser's keydown event caused by non-printable keys:
[1] A keypress event is fired and its keyCode and charCode are 0x10 (16) but the text isn't actually entered into the editor.
[2] On Mac, the Help key is mapped to the Insert key of PC keyboards. These keyCode values are the same as the Insert key's keyCode values.
[3] Tested on Fedora 20.
[4] Only a keyup event is fired.
[5] PC's PrintScreen, ScrollLock and Pause are mapped to Mac's F13, F14 and F15, respectively. Chrome and Safari map them to the same keyCode values as Mac's keys.
[6] Pause key with Control generates 0x03 (3).
keyCode values of each browser's keydown event caused by function keys:
[2] On PCs, PrintScreen, ScrollLock and Pause are mapped to the Mac's F13, F14 and F15, respectively. Firefox sets for them the same keyCode values as the PC's keys.
[3] Tested on Fedora 20. The keys don't cause GDK_Fxx keysyms. If the keys cause proper keysyms, these values must be same as IE.
[4] Tested on Fedora 20. The keys don't cause DOM key events on Chromium.
[5] The keyCode value of a keyUp event is 0x83 (131).
[6] Tested on Fedora 20. The keys don't cause DOM key events on Firefox.
[7] Only the keydown event is fired.
[8] No DOM key events are fired on Firefox.
Numpad keys
keyCode values of each browser's keydown event caused by keys in numpad in NumLock state
Note: Recent Mac doesn't have a NumLock key, and therefore state. That's why the unlocked state is not available.
Constants for keyCode value
Gecko defines a lot of keyCode values in KeyboardEvent for making the mapping table explicitly. These values are useful for add-on developers of Firefox, but not so useful in public web pages.
Constant
Value
Description
DOM_VK_CANCEL
0x03 (3)
Cancel key.
DOM_VK_HELP
0x06 (6)
Help key.
DOM_VK_BACK_SPACE
0x08 (8)
Backspace key.
DOM_VK_TAB
0x09 (9)
Tab key.
DOM_VK_CLEAR
0x0C (12)
"5" key on Numpad when NumLock is unlocked. Or on Mac, clear key which is positioned at NumLock key.
DOM_VK_RETURN
0x0D (13)
Return/enter key on the main keyboard.
DOM_VK_ENTER
0x0E (14)
Reserved, but not used. Deprecated (Dropped, see [Firefox bug 969247](https://bugzil.la/969247).)
DOM_VK_SHIFT
0x10 (16)
Shift key.
DOM_VK_CONTROL
0x11 (17)
Control key.
DOM_VK_ALT
0x12 (18)
Alt (Option on Mac) key.
DOM_VK_PAUSE
0x13 (19)
Pause key.
DOM_VK_CAPS_LOCK
0x14 (20)
Caps lock.
DOM_VK_KANA
0x15 (21)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_HANGUL
0x15 (21)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_EISU
0x 16 (22)
"英数" key on Japanese Mac keyboard.
DOM_VK_JUNJA
0x17 (23)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_FINAL
0x18 (24)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_HANJA
0x19 (25)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_KANJI
0x19 (25)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_ESCAPE
0x1B (27)
Escape key.
DOM_VK_CONVERT
0x1C (28)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_NONCONVERT
0x1D (29)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_ACCEPT
0x1E (30)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_MODECHANGE
0x1F (31)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_SPACE
0x20 (32)
Space bar.
DOM_VK_PAGE_UP
0x21 (33)
Page Up key.
DOM_VK_PAGE_DOWN
0x22 (34)
Page Down key.
DOM_VK_END
0x23 (35)
End key.
DOM_VK_HOME
0x24 (36)
Home key.
DOM_VK_LEFT
0x25 (37)
Left arrow.
DOM_VK_UP
0x26 (38)
Up arrow.
DOM_VK_RIGHT
0x27 (39)
Right arrow.
DOM_VK_DOWN
0x28 (40)
Down arrow.
DOM_VK_SELECT
0x29 (41)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_PRINT
0x2A (42)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_EXECUTE
0x2B (43)
Linux support for this keycode was added in Gecko 4.0.
DOM_VK_PRINTSCREEN
0x2C (44)
Print Screen key.
DOM_VK_INSERT
0x2D (45)
Ins(ert) key.
DOM_VK_DELETE
0x2E (46)
Del(ete) key.
DOM_VK_0
0x30 (48)
"0" key in standard key location.
DOM_VK_1
0x31 (49)
"1" key in standard key location.
DOM_VK_2
0x32 (50)
"2" key in standard key location.
DOM_VK_3
0x33 (51)
"3" key in standard key location.
DOM_VK_4
0x34 (52)
"4" key in standard key location.
DOM_VK_5
0x35 (53)
"5" key in standard key location.
DOM_VK_6
0x36 (54)
"6" key in standard key location.
DOM_VK_7
0x37 (55)
"7" key in standard key location.
DOM_VK_8
0x38 (56)
"8" key in standard key location.
DOM_VK_9
0x39 (57)
"9" key in standard key location.
DOM_VK_COLON
0x3A (58)
Colon (":") key.
DOM_VK_SEMICOLON
0x3B (59)
Semicolon (";") key.
DOM_VK_LESS_THAN
0x3C (60)
Less-than ("<") key.
DOM_VK_EQUALS
0x3D (61)
Equals ("=") key.
DOM_VK_GREATER_THAN
0x3E (62)
Greater-than (">") key.
DOM_VK_QUESTION_MARK
0x3F (63)
Question mark ("?") key.
DOM_VK_AT
0x40 (64)
Atmark ("@") key.
DOM_VK_A
0x41 (65)
"A" key.
DOM_VK_B
0x42 (66)
"B" key.
DOM_VK_C
0x43 (67)
"C" key.
DOM_VK_D
0x44 (68)
"D" key.
DOM_VK_E
0x45 (69)
"E" key.
DOM_VK_F
0x46 (70)
"F" key.
DOM_VK_G
0x47 (71)
"G" key.
DOM_VK_H
0x48 (72)
"H" key.
DOM_VK_I
0x49 (73)
"I" key.
DOM_VK_J
0x4A (74)
"J" key.
DOM_VK_K
0x4B (75)
"K" key.
DOM_VK_L
0x4C (76)
"L" key.
DOM_VK_M
0x4D (77)
"M" key.
DOM_VK_N
0x4E (78)
"N" key.
DOM_VK_O
0x4F (79)
"O" key.
DOM_VK_P
0x50 (80)
"P" key.
DOM_VK_Q
0x51 (81)
"Q" key.
DOM_VK_R
0x52 (82)
"R" key.
DOM_VK_S
0x53 (83)
"S" key.
DOM_VK_T
0x54 (84)
"T" key.
DOM_VK_U
0x55 (85)
"U" key.
DOM_VK_V
0x56 (86)
"V" key.
DOM_VK_W
0x57 (87)
"W" key.
DOM_VK_X
0x58 (88)
"X" key.
DOM_VK_Y
0x59 (89)
"Y" key.
DOM_VK_Z
0x5A (90)
"Z" key.
DOM_VK_WIN
0x5B (91)
Windows logo key on Windows. Or Super or Hyper key on Linux.
DOM_VK_CONTEXT_MENU
0x5D (93)
Opening context menu key.
DOM_VK_SLEEP
0x5F (95)
Linux support for this keycode was added in Gecko 4.0.
Attn (Attention) key of IBM midrange computers, e.g., AS/400.
DOM_VK_CRSEL
0xF7 (247)
CrSel (Cursor Selection) key of IBM 3270 keyboard layout.
DOM_VK_EXSEL
0xF8 (248)
ExSel (Extend Selection) key of IBM 3270 keyboard layout.
DOM_VK_EREOF
0xF9 (249)
Erase EOF key of IBM 3270 keyboard layout.
DOM_VK_PLAY
0xFA (250)
Play key of IBM 3270 keyboard layout.
DOM_VK_ZOOM
0xFB (251)
Zoom key.
DOM_VK_PA1
0xFD (253)
PA1 key of IBM 3270 keyboard layout.
DOM_VK_WIN_OEM_CLEAR
0xFE (254)
Clear key, but we're not sure the meaning difference from DOM_VK_CLEAR.
OEM specific keys on Windows
On Windows, some values of virtual keycode are defined (reserved) for OEM specific key. They are available for special keys on non-standard keyboard. In other words, some values are used for different meaning by two or more vendors (or hardware).
Starting Gecko 21 (and older than 15), OEM specific key values are available on the keyCode attribute only on Windows. So they are not useful for usual web applications. They are useful only for intranet applications or in similar situations.