key follows the layout, code follows the board
event.key is the character the key produces: "a", "A", "Enter", " " . It changes with the layout and with Shift. event.code is the physical key: KeyA, Digit1, Enter, Space, and it does not change with either. A shortcut meant to be found on the keyboard by its letter uses key; a game that means "the key under the left hand" uses code.
The trap is mixing them: a handler on code === "KeyZ" that then compares key === "z" breaks the moment Shift is held, and a handler on key === "z" stops responding on a QWERTZ keyboard where Z sits where Y sits here. Choose one axis and stay on it.
keyCode is a number with a history
keyCode predates both and was never standardised: it is 65 to 90 for letters, 48 to 57 for digits, 186 to 222 for punctuation with several disagreements between browsers, and 0 or missing for many keys. It is deprecated, it still works, and a lot of code in the world branches on it. New code should use key and code; the live value is printed here so that old code can be checked against the browser it will actually run in.
Modifiers, platform and the truth about Command
Four booleans arrive with every event: ctrlKey, altKey, shiftKey, metaKey. On macOS, Meta is Command. On Windows and Linux, Meta is the Windows or Super key, which browsers often keep for themselves, so a shortcut that should feel native accepts ctrlKey || metaKey and labels itself accordingly. This page prints the flags as they arrived and never guesses the platform.
A modifier pressed on its own also fires a keydown, with key equal to "Shift" and so on. That is why a handler that only looks at key can fire on the modifier before the letter arrives, and why the generated listener checks the exact modifier set instead of "any of these".
location, and the two Enter keys
event.location is 0 for the main block, 1 for the left modifier, 2 for the right, 3 for the numpad. A numpad 5 is code: "Numpad5", key: "5", location: 3, and it fires as a digit even with Num Lock off on some systems, where it fires as Clear. A form that treats numpad Enter as submit usually wants key === "Enter" and not a location check.
Composition, and the shortcut that fires while typing
A Korean or Japanese input method composes a syllable from several key presses before it commits. During composition, event.isComposing is true and key is often "Process". A shortcut handler that ignores this can fire in the middle of a word, which is why real editors check isComposing and often the keyCode === 229 that older browsers send for the same reason.
The same care applies to preventDefault: it stops the browser's default action, not the operating system's. Ctrl+W, Ctrl+T, Cmd+Tab and the Windows key cannot be intercepted from a page, and a page that listens to every key outside a focused field will eventually break a scroll, a back gesture or a screen reader command.
When the key produces a character and the question is which code point it is, Pikkola Unicode shows the code points. When the question is what runs after the handler, Pikkola Event shows the timer and the queue.