What is a keycode and what does a key press tell you?
Every time you press a key in a web page, the browser fires a keydown event and fills a KeyboardEvent object with several different answers to the question "which key was that?". They are easy to confuse. key is the character or name the key produces with the current layout and modifiers (a, A, Enter, ç). code is the physical key on the keyboard, independent of layout (KeyA, Enter, Semicolon). keyCode is an old number, deprecated but still everywhere in legacy code (65 for the letter A). location says which copy of a key you pressed: left, right or numeric keypad.
This tool is a key inspector. Click on the page, press any key and it shows all of those values at once, plus the modifiers that were held, whether the event is an auto-repeat, and the same key in three hardware tables: the Linux evdev code, the USB HID usage and the PS/2 scan code set 1. Those last three are what you need when you configure a remapper, write a keyboard firmware, bind a key in a Linux desktop or read an evtest log, and they are hard to find in one place.
The page listens to the keyboard only while you have it open and does not record, store or send what you press. It is meant for reading key values while you develop, so avoid it for typing passwords or anything private.
How to find the keycode of a key
- Open the page and click anywhere in it, so the browser sends the keyboard events to the page and not to the address bar.
- Press the key you want to inspect. The big area shows the key that was pressed, and the list below shows the details. Press another key to replace them.
- Read the first four rows for web code: Key (
event.key), Keycode (event.keyCode), Code (event.code) and Location (0 standard, 1 left, 2 right, 3 numeric keypad). - Check Modifiers: it lists Meta, Shift, Ctrl and Alt when they are held while you press, joined with a plus sign. The text says None when no modifier is down.
- Read the three hardware rows when you need them. Linux evdev gives the constant name and its number (for example
KEY_A (30)), USB HID usage gives the usage ID in hexadecimal and decimal and PS/2 set 1 gives the make code. If the key is not in the built-in table, the row says so instead of guessing. - Hold a key down and look at Repeat: it becomes true for the auto-repeated events after the first one.
- Use Copy as JSON to paste all the values in a bug report. The JSON keys are always in English.
Examples
The letter A
Pressing A without modifiers gives:
{
"Key": "a",
"Keycode": "65",
"Code": "KeyA",
"Location": "0",
"Modifiers": "",
"Linux evdev": "KEY_A (30)",
"USB HID usage": "0x04 (4)",
"PS/2 set 1": "0x1E",
"Repeat": "false"
}With Shift held, Key becomes A but Code stays KeyA: the physical key is the same.
Two Enter keys, one value for the keyCode
The main Enter reports Enter, location 0 and KEY_ENTER (28). The Enter of the numeric keypad reports the same Key (Enter) and the same Keycode (13), but Code is NumpadEnter, location is 3 and the hardware rows are KEY_KPENTER (96), 0x58 (88) and, in PS/2, the extended code 0xE0 0x1C. This is the usual reason to prefer code when the two keys must behave differently.
Left Shift and the arrow keys
Left Shift reports Key Shift, Code ShiftLeft, location 1, keycode 16 and evdev KEY_LEFTSHIFT (42). The left arrow reports ArrowLeft, keycode 37, KEY_LEFT (105), USB HID 0x50 (80) and PS/2 0xE0 0x4B. Arrow keys are the extended keys: their PS/2 codes start with 0xE0.
Same physical key, different layouts
On a US keyboard the key to the right of L types ; and its Code is Semicolon. On a Brazilian ABNT2 or a Spanish keyboard the same physical key types ç or ñ, so Key changes but Code is still Semicolon. For a shortcut that must sit in the same place for everyone, such as W, A, S and D movement in a game, use code; for the character the person typed, use key.
Common problems and how to fix them
- Nothing appears when you press a key: click on the page first. If the focus is in the address bar or in another window, the page receives nothing.
- The browser reacts to the key instead (Tab moves the focus, F5 reloads): browsers keep some keys for themselves, and the system reserves others (
Cmd+Tab,Win). Those cannot be inspected reliably in a page. - The keycode is 229: this number means that an input method (IME), used for Chinese, Japanese, Korean or dead-key accents, is composing text. Use
event.keyand the composition events instead ofkeyCode. - The hardware rows say Not in the table: the key is not one of the common keys of the built-in table, such as a media or vendor key. The web values still work.
- The values differ between Windows, macOS and Linux for the same key: the web values are normalised by browsers, but special keys, the Meta key and international layouts can still report differently. Test on each system that matters to you, and add your platform from device information to the report.
Frequently asked questions
What is the keycode of the Enter key?
event.keyCode is 13 for Enter and for the numeric keypad Enter, event.key is Enter and event.code is Enter or NumpadEnter. Press the key here to confirm on your keyboard.
What is the difference between event.key and event.code?
key is the value the key produces, taking layout, Shift and Caps Lock into account. code is the physical position of the key. A French keyboard and a US keyboard give different key values for the same physical key but the same code.
Should I still use keyCode?
No. keyCode is deprecated and ambiguous, as the two Enter keys show. Use key for characters and code for positions. The number remains only for old code that you maintain.
How do I read a key press in JavaScript?
Add a keydown listener and read event.key and event.code, as in the example below. To work with the number of a character, see text to Unicode, and to convert hexadecimal values such as 0x1E use the base converter.
Doing it in code
JavaScript
document.addEventListener('keydown', (event) => {
console.log(event.key, event.code, event.location, event.repeat);
if (event.code === 'KeyW' && !event.repeat) {
console.log('move up'); // same physical key on any layout
}
if (event.key === 'Escape') {
console.log('close the dialog');
}
});