You add a keyboard shortcut to a web app, and it works perfectly on your machine. Then a colleague with a German keyboard reports that Ctrl+Z does the wrong thing, a French user says the WASD controls in your demo are scattered across the keyboard, and a code review flags keyCode as deprecated. Keyboard handling in JavaScript has three generations of properties, and choosing the right one depends on what you are trying to detect.
This guide explains what event.key, event.code, keyCode and which each report, when to use which, how modifier keys and repeats work, and the edge cases that catch people out.
Three ways to identify a key
When a key is pressed, the browser fires a KeyboardEvent. The same physical press can be described in different ways.
event.key: the character or action
event.key is the value the key produces, taking the keyboard layout and modifiers into account. Pressing the key labelled A gives "a", or "A" with Shift. Non-printing keys have names: "Enter", "Escape", "ArrowLeft", "Backspace", "Tab", " " (a single space for the space bar).
Use it when meaning matters: "the user typed a question mark", "the user pressed Escape".
event.code: the physical position
event.code names the physical key on a standard US QWERTY layout, regardless of what the current layout prints on it. The key to the right of Tab is always "KeyQ", even on a French AZERTY keyboard where it types "a". Other examples: "Digit1", "Space", "ShiftLeft", "Numpad5".
Use it when position matters: game controls, piano-style keyboards, or shortcuts designed around hand placement.
keyCode and which: the legacy numbers
keyCode (and its twin which) are numeric codes from early browsers: 13 for Enter, 27 for Escape, 65 for A. They are deprecated. Their values were never fully standardised, differ for some punctuation keys between browsers and layouts, and do not distinguish left from right modifiers. They still work in current browsers for compatibility, so you will meet them in older code, but new code should use key or code.
Comparing them side by side
| Pressed on a US layout | event.key | event.code | keyCode |
|---|---|---|---|
| A | a |
KeyA |
65 |
| Shift + A | A |
KeyA |
65 |
| Enter | Enter |
Enter |
13 |
| Numpad Enter | Enter |
NumpadEnter |
13 |
| Left Shift | Shift |
ShiftLeft |
16 |
| Space bar | " " |
Space |
32 |
| 1 on the number row | 1 |
Digit1 |
49 |
The Numpad Enter and Left Shift rows show why code exists: key and keyCode cannot tell those keys apart.
Find the values for any key
Rather than memorising tables, press the key and look. The JavaScript Keycode Finder shows every property for the key you press.
- Open the JavaScript Keycode Finder and click anywhere on the page so it has keyboard focus. On a phone or tablet you need a physical keyboard.
- Press a key. The large number is
event.keyCode, withevent.keyandevent.codeshown beside it. - Read the detail tiles: event.key, event.code, event.keyCode, event.which, event.location (Standard, Left, Right or Numpad) and event.repeat. Below them,
shiftKey,ctrlKey,altKeyandmetaKeylight up when held. - Switch on Block default browser action (scrolling, quick find) while testing if keys like the arrows, space or slash are scrolling the page or opening search. Ctrl/Cmd combinations and Tab are always left alone, so you can still switch tabs or copy.
- Copy the ready-made JavaScript condition for the last key, and use the Recent list to compare several presses.
- Filter the Key code reference (US QWERTY) table by key, code or number when you do not have the key at hand.
Writing keyboard handlers
A typical handler combining the different properties:
document.addEventListener("keydown", (event) => {
// Ignore keys typed into form fields
if (event.target.closest("input, textarea, select, [contenteditable]")) return;
// Save shortcut: Ctrl+S on Windows/Linux, Cmd+S on macOS
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "s") {
event.preventDefault();
saveDraft();
return;
}
// Game-style movement: physical position matters, not the letter
if (event.code === "KeyW") moveUp();
if (event.code === "KeyS") moveDown();
// Close a dialog
if (event.key === "Escape") closeDialog();
});
Here saveDraft, moveUp, moveDown and closeDialog stand for your own functions.
keydown, keyup and keypress
Use keydown for shortcuts and controls; it fires for every key, including arrows and modifiers. keyup fires on release and is useful for "hold to do something" interactions. keypress is deprecated and does not fire for non-printing keys; do not use it in new code. For capturing text the user types, listen to the input event on the field instead, which also covers paste, autocomplete, dictation and on-screen keyboards.
Handling repeats and IME input
Holding a key fires repeated keydown events with event.repeat set to true. Check it when an action should happen only once per press.
Users typing Chinese, Japanese, Korean and other languages often use an input method editor (IME), where several keystrokes build one character. During composition event.isComposing is true, and an Enter press may be confirming a character rather than submitting a form:
document.addEventListener("keydown", (event) => {
if (event.isComposing) return; // an IME is still building a character
if (event.key === "Enter" && !event.repeat) submitSearch();
});
Choosing key or code for shortcuts
For letter shortcuts like Ctrl+S or Ctrl+Z, event.key usually matches user expectations: people press the key that shows the letter. Some applications prefer event.code so that shortcuts stay in the same place on every layout. Either choice is defensible; be consistent and show the shortcut in your UI using the character the user sees.
On macOS, use metaKey (Cmd) where Windows and Linux use ctrlKey. Also note that Alt/Option combinations on a Mac often produce special characters, so event.key for Option+S is not "s".
Pitfalls and troubleshooting
Some keys never reach the page
Browsers and operating systems reserve certain combinations, such as Ctrl+W, Ctrl+T, Cmd+Q and the Windows key. You cannot reliably override them with preventDefault().
Accessibility conflicts
Single-letter shortcuts without modifiers can clash with screen reader and speech-input commands. WCAG asks that such shortcuts can be turned off or remapped, or only work when the relevant component has focus. Also never break Tab navigation.
Physical keyboard testing
Hardware problems (a key that double-types or does not register) are a different issue; the keyboard tester shows every key on a visual layout. For character codes rather than key codes, the ASCII table lists values like 65 for "A", which happens to match keyCode for letters but is a different concept. When you need to put special characters into strings, the string escape tool handles the escaping, and the user agent parser helps when a bug only appears in one browser or OS.
Frequently asked questions
Is keyCode deprecated?
Yes. It still works in browsers for compatibility, but the standard recommends event.key or event.code because keyCode values are inconsistent across layouts and browsers.
What is the difference between event.key and event.code?
event.key is the character or action produced, affected by layout and Shift. event.code is the physical key position, which stays the same on every layout.
How do I detect the Enter key in JavaScript?
Check event.key === "Enter" in a keydown listener. It matches both the main Enter key and the numpad Enter; use event.code if you must tell them apart.
Why does my shortcut not work on a non-English keyboard?
If you check event.code, the key may be in a different place from the printed letter; if you check event.key, the layout may produce a different character. Test with the layouts your users have.
How do I detect Ctrl or Cmd?
Use the boolean properties event.ctrlKey and event.metaKey. For a cross-platform shortcut, accept either one.