How to find a JavaScript key code
- Press any key on your keyboard while this page is focused. On a phone or tablet, tap the box to bring up the on-screen keyboard.
- The key is shown in large type, with
event.key,event.code, the legacykeyCodeandwhichvalues, the key location (left, right or numpad), active modifiers and whether the key is auto-repeating. - Copy the ready-made
keydownhandler snippet for that key or shortcut, including Ctrl, Cmd, Alt and Shift checks.
key vs code vs keyCode
event.key is the character or name the key produces, taking the keyboard layout and Shift into account: "a", "A", "Enter", "ArrowUp". Use it for shortcuts and text-related logic.
event.code identifies the physical key position regardless of layout: "KeyA" is the key next to Caps Lock on a QWERTY board, even if a French AZERTY layout types "q" there. Use it for games and WASD-style controls.
keyCode and which are deprecated numeric codes. They vary between browsers and layouts and should not be used in new code, but you will still meet them in older libraries and Stack Overflow answers, so they are shown here for reference.
Some keys never reach web pages, such as the Fn key and certain operating-system shortcuts. Mobile virtual keyboards often report Unidentified or Process for letters; in that case the typed character is shown from the input event instead.
Key presses are only shown on this page and are never recorded or sent anywhere.
What is the keyCode for Enter?
13. In modern code, check e.key === "Enter" instead.
How do I detect Ctrl+S or Cmd+S?
Check (e.ctrlKey || e.metaKey) && e.key === "s" in a keydown handler and call e.preventDefault() to stop the browser's save dialog.
Why does Escape not close my modal on mobile?
Most phone keyboards have no Escape key. Provide a visible close button as well as keyboard shortcuts.