event.key vs. event.code
Published 2026-08-11 · Updated 2026-08-11
Browser keyboard events carry two different identifiers. event.code names the physical key position, such as KeyA or ShiftLeft, while event.key names the value the current layout and modifier state produce, such as a, A, or Shift.
A layout example
On a US ANSI keyboard, the key to the left of 1 is Backquote and usually produces `. On a UK ISO keyboard, that position may produce ` or ¬ depending on the input method. The physical code stays Backquote while event.key changes.
This distinction makes event.code useful for testing the physical switch. It is also why a keyboard test should show both values: one tells you whether the switch fired, and the other tells you what character the operating system interpreted.
When they disagree
Press Shift + 2 on a US layout and event.code is Digit2 while event.key is @. Press the same key without Shift and event.key is 2. The switch is the same; the produced value is not.
Related guides
Try it now
Open the workspace and run the steps from this guide against your own keyboard.
Open the workspace