How to use the JavaScript Keycode Finder
- 1. Focus the boxClick it or Tab to it.
- 2. Press a keyAny key or combination, like Ctrl + S.
- 3. Copy the codeUse the ready if (event.key === …) check.
key vs code vs keyCode
- event.key is the character or action the key produces: "a", "A" with Shift, "Enter", "ArrowUp". It follows the user's keyboard layout. Use it for shortcuts and text.
- event.code is the physical key position: "KeyA" is always the key where A sits on a US keyboard, whatever the layout. Use it for games with WASD controls.
- event.keyCode and event.which are old number codes (Enter is 13). They are deprecated, but you will still find them in older code and interview questions.
- event.location tells left from right Shift, Ctrl and Alt (1 or 2) and numpad keys (3).
Worked example
Press Enter and you see event.key "Enter", event.code "Enter" and keyCode 13, plus the snippet:
document.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
// your code
}
});Press Shift + A and event.key is "A" with Shift listed under modifiers, while event.code stays "KeyA". The last 10 keys you pressed are kept in a small table so you can compare them.
Tab normally moves focus away so keyboard users are never trapped. Tick Also capture Tab to see Tab's values; Esc turns that off again. Some combinations, like Ctrl + W or the Windows key, are taken by the browser or operating system before the page sees them.
Frequently asked questions
Should I use keydown or keypress?
keydown. The keypress event is deprecated and doesn't fire for keys like arrows or Escape.
Why is keyCode deprecated?
Its numbers differ between browsers and layouts. event.key and event.code are standard and clearer.
Does it work on phones?
On-screen keyboards often report "Unidentified" or 229 for letters, so test shortcuts on a physical keyboard.
Is the javascript keycode finder free?
Yes, it is free. Pressing your first key asks for your name and WhatsApp number once. After that, the javascript keycode finder and every other Uncodemy tool open with no form.