Home › Guides › Developer › KeyboardEvent key vs code

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

PropertyDescribesExampleChanges with layout or Shift?
keyThe logical value of the press"a", "A", "Enter", "ArrowUp"Yes
codeThe physical key"KeyA", "Digit1", "Space"No
locationWhich copy of the key0 standard, 1 left, 2 right, 3 numpadNo
keyCodeLegacy number (deprecated)65 for the A keyInconsistent

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:

LayoutLetter printed on the keykeycode
US QWERTYQ"q""KeyQ"
French AZERTYA"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 presskeycodelocation
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

Special key values

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

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.

Sources