files/en-us/web/api/keyboardevent/keycode/index.md
{{APIRef("UI Events")}}{{Deprecated_Header}}
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 {{domxref("KeyboardEvent.code")}} (for the physical key pressed) or {{domxref("KeyboardEvent.key")}} (for the character the key maps to). Check compatibility for either property if you target very old browsers.
[!NOTE] Web developers shouldn't use the
keyCodeattribute for printable characters when handlingkeydownandkeyupevents. As described above, thekeyCodeattribute is not useful for printable characters, especially those input with the <kbd>Shift</kbd> or <kbd>Alt</kbd> key pressed.
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 {{Glossary("ASCII")}} characters inputtable by the key — even with shift modifiers or an ASCII capable keyboard layout. See the following rules for details:
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
keyCodevalues may be conflict with other keys. For example, if the active keyboard layout is Russian, thekeyCodevalue of both the"Period"key and"Slash"key are190(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 {{domxref("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.
keyCode for the character.keyCode for the shifted character.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:
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.[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).
[1] On Windows, pressing the <kbd>AltGraph</kbd> key raises both the "ControlLeft" and the "AltRight" key events.
[2] When the Japanese keyboard layout is active, pressing the <kbd>CapsLock</kbd> key without pressing <kbd>Shift</kbd> raises 0xF0 (240). The key works as the <kbd>Alphanumeric</kbd> key whose label is "英数".
[3] When the Japanese keyboard layout is active, pressing the <kbd>"CapsLock"</kbd> key without pressing <kbd>Shift</kbd> raises 0x00 (0). The key works as the <kbd>"Alphanumeric"</kbd> key whose label is "英数".
[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 <kbd>Help</kbd> key is mapped to the <kbd>Insert</kbd> key of PC keyboards. These keyCode values are the same as the <kbd>Insert</kbd> key's keyCode values.
[3] Tested on Fedora 20.
[4] Only a keyup event is fired.
[5] PC's <kbd>PrintScreen</kbd>, <kbd>ScrollLock</kbd> and <kbd>Pause</kbd> are mapped to Mac's <kbd>F13</kbd>, <kbd>F14</kbd> and <kbd>F15</kbd>, respectively. Chrome and Safari map them to the same keyCode values as Mac's keys.
[6] <kbd>Pause</kbd> key with <kbd>Control</kbd> generates 0x03 (3).
[1] Tested on Fedora 20.
[2] On PCs, <kbd>PrintScreen</kbd>, <kbd>ScrollLock</kbd> and <kbd>Pause</kbd> are mapped to the Mac's <kbd>F13</kbd>, <kbd>F14</kbd> and <kbd>F15</kbd>, 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.
[1] "NumLock" key works as "Clear" key on Mac.
[!NOTE] Recent Mac doesn't have a <kbd>NumLock</kbd> key, and therefore state. That's why the unlocked state is not available.
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.
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;
} else if (event.keyCode !== undefined) {
// Handle the event with KeyboardEvent.keyCode
handled = true;
}
if (handled) {
// Suppress "double action" if event handled
event.preventDefault();
}
});
{{Specifications}}
{{Compat}}
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.
See "Manufacturer-specific Virtual-Key Codes (Windows CE 5.0)" in MSDN for the detail.