KeyboardEvent key vs code explained
How event.key and event.code differ, when to use each, how layouts change them and why keyCode is deprecated.
By the tools4free.dev team · Last reviewed 4 December 2026
event.key is the value a key press produces, such as "a", "A" or "Enter", and it changes with the keyboard layout, Shift and other modifiers. event.code identifies the physical key, such as "KeyA", and it does not change with the layout. Use key when you care about the character or the meaning of the press, and code when you care about where the key sits. The old keyCode and which properties are deprecated and should not be used in new code.
What each property means
| Property | Describes | Example | Changes with layout or Shift? |
|---|---|---|---|
key | The logical value of the press | "a", "A", "Enter", "ArrowUp" | Yes |
code | The physical key | "KeyA", "Digit1", "Space" | No |
location | Which copy of the key | 0 standard, 1 left, 2 right, 3 numpad | No |
keyCode | Legacy number (deprecated) | 65 for the A key | Inconsistent |
The W3C UI Events specification defines key values and the separate UI Events KeyboardEvent code specification defines code values. The code names describe the position on a standard US-style keyboard, so KeyA is the key where US keyboards print A, whatever is printed on your keycap.
Worked example 1: the same physical key on three layouts
Press the key that sits to the right of Tab, where a US QWERTY keyboard prints Q. The browser reports the same code on every layout and a different key:
| Layout | Letter printed on the key | key | code |
|---|---|---|---|
| US QWERTY | Q | "q" | "KeyQ" |
| French AZERTY | A | "a" | "KeyQ" |
| Dvorak | ' (apostrophe) | "'" | "KeyQ" |
We checked the description of such an event with the key-description code that powers our key code tool: for key: 'a', code: 'KeyQ' it reports "Letter Q", because the name comes from the code. The consequence is practical. A game that binds movement to KeyW, KeyA, KeyS and KeyD keeps the familiar diamond shape on an AZERTY keyboard, where those same positions print Z, Q, S and D. A text shortcut such as "press S to save" should look at key, so it matches the letter the user sees on the keycap.
Worked example 2: Shift, digits and the numpad
The values below are what a browser reports on a US layout. Nothing here changes code when Shift is held; only key moves.
| Physical press | key | code | location |
|---|---|---|---|
| A | "a" | "KeyA" | 0 |
| Shift + A | "A" | "KeyA" | 0 |
| 1 on the top row | "1" | "Digit1" | 0 |
| Shift + 1 on the top row | "!" | "Digit1" | 0 |
| 1 on the numpad, Num Lock on | "1" | "Numpad1" | 3 |
| 1 on the numpad, Num Lock off | "End" | "Numpad1" | 3 |
| Space bar | " " | "Space" | 0 |
| Left Shift | "Shift" | "ShiftLeft" | 1 |
| Enter on the main block | "Enter" | "Enter" | 0 |
| Enter on the numpad | "Enter" | "NumpadEnter" | 3 |
Two details deserve attention. First, the space bar's key is a single space character, not the word "Space". Second, the numpad digit with Num Lock off reports key: "End" but still code: "Numpad1", which is why code-based handling can tell the two Enter keys apart and key-based handling cannot. We ran the same code values through the tool's naming function in Node: Numpad1 gives "Numpad 1", NumpadEnter gives "Numpad Enter" and AltRight gives "Right Alt".
Choosing between key and code
- Typing, shortcuts named by a letter, anything shown to the user: use
key. "Ctrl + S" means the key labelled S. - Games, editors with spatial layouts, any control defined by position: use
code. WASD movement should follow the position. - Named non-character keys such as
"Escape","Tab"and"ArrowLeft":keyis easy to read and is the same on every layout. Both properties work. - Text input: do not rebuild text from key events at all. Use the
inputevent, which also handles pasted text, voice input and input method editors.
Special key values
"Dead": a dead key, such as the accent key that waits for the next letter."Unidentified": the browser could not map the key."Process": an input method editor is composing text, as with Chinese, Japanese and Korean input. Checkevent.isComposingand skip your shortcut handling while it is true, or pressing Enter to confirm a candidate will trigger your Enter action."AltGraph": reported for the right Alt key on layouts that use it as a third-level shifter, such as German."Meta": the Windows key or the Command key.
Holding a key sends repeated keydown events with event.repeat set to true. Test that flag if an action should fire once per press.
Why keyCode is deprecated
keyCode and which are numbers that differ between browsers, layouts and operating systems for anything but the simplest keys. The letter A gives 65, Enter 13, Escape 27, Space 32 and the arrows 37 to 40, but punctuation keys disagree widely, and a numpad key and the matching top-row key give different numbers. MDN marks both as deprecated, and the numbers carry no information about the layout or the modifier state. Replace them like this:
// Old
if (e.keyCode === 27) closeDialog();
// Current
if (e.key === 'Escape') closeDialog();
// Position-based
if (e.code === 'KeyW') moveUp();
Our key code tool shows the current properties and states plainly that the legacy ones are not used. If you maintain older code, the one place keyCode still turns up is the value 229, which browsers send for events fired during IME composition.
Writing a layout-friendly handler
A small habit removes most layout bugs: decide for each binding whether it is a character or a position, and name it that way in your code. Keep a map from action names to code values for game controls, let players remap them, and show the current binding using the key value the user would recognise. Where the Keyboard API's layout map is available you can also look up which character a given code produces on the current layout, but support is limited, so treat it as an enhancement.
Common mistakes
- Using
codefor shortcuts that mention a letter. The shortcut then fires for the wrong key on non-QWERTY layouts. - Using
keyfor WASD movement. It breaks on AZERTY and other layouts. - Comparing
keyto an uppercase letter. With Caps Lock or Shift the value is"A"; without it,"a". Normalise the case or check the modifier. - Writing
e.key === 'Space'. The value is" ". The word"Space"is acode. - Ignoring IME composition. Check
isComposingbefore acting on Enter. - Using the old
"Left"and"Esc"names. Very old browsers sent them. Current names are"ArrowLeft"and"Escape". - Ignoring modifier keys. Check
ctrlKey,metaKey,altKeyandshiftKeyso Ctrl + S is not treated as a plain S.
Frequently asked questions
Can I get the character a key will type from code?
No. code tells you the position only. Use key, or read the input event for text.
Which event should I listen to: keydown or keyup?
keydown for actions that should start immediately and keyup when the release matters. The old keypress event is deprecated, so do not use it.
Why does a keyboard shortcut not work on my user's keyboard?
Usually because the handler checks code where the label matters, or the browser or operating system has already claimed the shortcut. Some combinations cannot be intercepted.
Do touch keyboards fire these events?
Often not in the same way. On-screen keyboards can report "Unidentified" and may skip key events entirely. Rely on input for text.
Try it: open the key code tool, press any key and see its key, code and location. Try the same key with Shift held to watch only key change.