Developer and Text Tools

event.key vs event.code vs keyCode: Keyboard Events Explained

Which keyboard event property to use in JavaScript, why keyCode is deprecated, how layouts change key but not code, and how to build accessible shortcuts.

Saizul Amin
Saizul Amin30 Oct 2026 · 3 min read
event.key vs event.code vs keyCode: Keyboard Events Explained

event.key vs event.code vs keyCode: Keyboard Events Explained

Keyboard handling looks simple until your users start using different keyboards. A shortcut that works on your laptop does nothing on a French keyboard. A game moves the wrong way for German players. An old script compares numbers like 13 and 27 and breaks on a numeric keypad. The free Keyboard Event Viewer shows exactly what the browser reports for every key you press, which is the fastest way to understand these differences.

Three ways to identify a key

When you press a key, the browser fires keydown and keyup events that carry several properties.

event.key is what the key means at that moment. Pressing the A key gives "a", or "A" with Shift. On a French AZERTY keyboard, the key in the same place gives "q". Non-character keys have names: "Enter", "Escape", "ArrowLeft", "F5". This is the right property for text input and for most shortcuts, because it follows what the user sees printed on their keys.

event.code is the physical position. The key to the right of Caps Lock is "KeyA" on every keyboard, whatever letter it types. Use code when position matters more than meaning, such as WASD movement in games or shortcuts laid out as a grid.

keyCode and which are numbers from older browsers: 65 for A, 13 for Enter, 27 for Escape. They are deprecated. They describe layouts poorly, and a few keys report different numbers in different browsers. Use them only to understand legacy code, and replace them when you can.

Location and modifiers

The location property separates keys that appear more than once: left and right Shift, Control, Alt and Meta, and the numeric keypad. That is how you tell the main Enter key from the keypad's Enter, whose code is "NumpadEnter".

Modifier state comes with every event as ctrlKey, altKey, shiftKey and metaKey. On a Mac, Meta is the Command key, so cross-platform shortcuts often check ctrlKey on Windows and Linux and metaKey on macOS.

Repeats and composition

Holding a key down fires repeated keydown events with repeat set to true. Ignore repeats if an action should run once per press.

People typing Chinese, Japanese or Korean use input method editors, which compose characters across several keystrokes. During composition, keydown reports isComposing as true and key as "Process". For text entry, listen to input events instead of keydown.

Keys you cannot capture

The operating system and the browser handle some combinations before the page sees them: closing tabs, switching windows, taking screenshots. Do not design features around them. If you capture a key that the browser normally uses, such as Ctrl+F for find, think hard about whether users expect the browser's behaviour.

Accessible shortcuts

Shortcuts help power users but can trap others. WCAG 2.1 requires that single-key shortcuts can be turned off or remapped, or only work when a component has focus, because speech-recognition users can trigger them accidentally. Document your shortcuts, avoid clashing with screen reader keys, and make every action reachable without a shortcut too.

Using the viewer

Click the box and press keys. The large display shows the last key with its key, code, keyCode, location and modifiers, and the log lists keydown and keyup events in order. The reference table lists the values for a US layout and can be searched by name or number.

Step by step

  1. Click inside the viewer box.
  2. Press a key or a combination.
  3. Read key, code, keyCode, location and modifiers.
  4. Compare keydown and keyup in the log.
  5. Search the reference table when updating old code.

Privacy

Key events stay in the page. Nothing you type is recorded or sent anywhere.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages