⌨️ JavaScript Key Code Finder
By ToolNimba Web Dev Team · Updated 2026-06-19
The capture box must be focused. Some shortcuts (like Ctrl+W or F5) may be intercepted by your browser.
| key | code | keyCode | modifiers |
|---|
Click the box above, then press any key on your keyboard.
Press any key and this tool shows you exactly what a JavaScript keydown event reports for it: the modern event.key and event.code, the legacy event.keyCode and event.which, the physical key location, and which modifier keys (Ctrl, Alt, Shift, Meta) were held at the time. It is the fastest way to look up the right value to test in a keyboard shortcut or game control handler, and it even builds a copy-ready listener snippet for the key you pressed. Everything runs in your browser, so your keystrokes never leave the page.
What is the JavaScript Keycode Finder?
Every time you press a key in a browser, the page can fire a KeyboardEvent. The two properties you actually want today are event.key and event.code. event.key is the logical value the key produces, so pressing the A key gives "a" (or "A" with Shift), and pressing Enter gives "Enter". event.code is the physical button on the keyboard regardless of layout or modifiers, so the same A key is always "KeyA" and the top-row number one is always "Digit1". Use event.key when you care about the character a user typed, and event.code when you care about the physical position, which is what you want for game controls like WASD that should work the same on any keyboard layout.
You will still see event.keyCode and event.which in older code and tutorials. These are numeric codes (for example 65 for the A key, 13 for Enter, 27 for Escape) and they are deprecated. They are kept here because plenty of existing code and many interview questions still reference them, and because reading the number can help you recognise a key quickly. For anything new, prefer event.key and event.code, which are readable strings and are not affected by the inconsistencies that plagued the old numeric codes across browsers.
The event also tells you the state of the modifier keys through the boolean flags event.ctrlKey, event.altKey, event.shiftKey and event.metaKey (the Windows key or the Command key on a Mac). A property called event.location distinguishes otherwise identical keys, so it can tell a left Shift from a right Shift, or the numpad Enter from the main Enter. Together these let you build precise shortcuts, for example only acting when event.ctrlKey is true and event.code is "KeyS" to catch Ctrl+S.
When to use it
- Finding the exact event.code string to test when wiring up a keyboard shortcut such as Ctrl+S or the arrow keys.
- Choosing physical key codes (KeyW, KeyA, KeyS, KeyD) for game movement so controls work across keyboard layouts.
- Debugging why a key handler does not fire by confirming what value the browser actually reports for that key.
- Teaching or learning the difference between event.key, event.code and the deprecated event.keyCode.
- Checking which modifier keys a browser registers, including the difference between left and right via event.location.
How to use the JavaScript Keycode Finder
- Click the capture box so it has keyboard focus.
- Press any key, or a key combination, on your keyboard.
- Read off event.key, event.code, the legacy event.keyCode and event.which, the location and any modifiers held.
- Copy the generated listener snippet to drop a ready-made check for that exact key into your code.
Formula & method
Worked examples
You press the letter A key with no modifiers.
- event.key reports "a" (the logical character produced)
- event.code reports "KeyA" (the physical key, layout-independent)
- event.keyCode reports 65 (legacy numeric code)
- All modifier flags are false
Result: key "a", code "KeyA", keyCode 65, no modifiers
You hold Ctrl and press S to detect a save shortcut.
- event.key reports "s" and event.code reports "KeyS"
- event.ctrlKey is true while the other modifier flags are false
- A correct guard is: if (e.ctrlKey && e.code === "KeyS") { ... }
- Call e.preventDefault() inside to stop the browser save dialog
Result: Combination Ctrl + S detected via e.ctrlKey and e.code === "KeyS"
Common keys and the values a keydown event reports for each
| Key pressed | event.key | event.code | event.keyCode (legacy) |
|---|---|---|---|
| A | a | KeyA | 65 |
| 1 (top row) | 1 | Digit1 | 49 |
| Enter | Enter | Enter | 13 |
| Escape | Escape | Escape | 27 |
| Spacebar | (a space) | Space | 32 |
| Left Arrow | ArrowLeft | ArrowLeft | 37 |
| Left Shift | Shift | ShiftLeft | 16 |
| F1 | F1 | F1 | 112 |
Common mistakes to avoid
- Using the deprecated event.keyCode in new code. event.keyCode and event.which are deprecated and were inconsistent across browsers. For new code use event.key for the character or event.code for the physical key, both of which are readable strings.
- Confusing event.key with event.code. event.key changes with layout and Shift (so "a" becomes "A"), while event.code is the fixed physical key ("KeyA"). For shortcuts and game controls that must ignore layout, use event.code.
- Forgetting to preventDefault on browser shortcuts. Keys like Ctrl+S, Ctrl+P or F5 trigger built-in browser actions. If you handle them, call event.preventDefault() inside your check, and note the browser may intercept some combinations before your code runs.
- Comparing the case of event.key without thinking about Shift. Because event.key returns "A" when Shift is held but "a" otherwise, a strict comparison to one case can miss the key. Lowercase the value, or use event.code which does not change with Shift.
Glossary
- KeyboardEvent
- The event object a browser fires for keydown, keypress and keyup, carrying all the key and modifier details.
- event.key
- The logical value the key produces, such as "a", "A", "Enter" or "ArrowLeft". It changes with layout and modifiers.
- event.code
- The physical key on the keyboard, such as "KeyA" or "Digit1", independent of layout, Shift or the produced character.
- event.keyCode
- A deprecated numeric code for the key (for example 65 for A). Kept for legacy code but no longer recommended.
- event.location
- A number telling which copy of a key was pressed: 0 standard, 1 left, 2 right, 3 numeric keypad.
- Modifier key
- A key like Ctrl, Alt, Shift or Meta (Windows or Command) whose held state is exposed as a boolean flag on the event.
Frequently asked questions
What is the difference between event.key and event.code?
event.key is the logical value the key produces, so it gives "a" or "A" depending on Shift and the keyboard layout. event.code is the physical key in a fixed position, so the same button is always "KeyA" regardless of layout or modifiers. Use event.key when you care about the typed character and event.code when you care about the physical key.
Is event.keyCode deprecated?
Yes. event.keyCode and event.which are deprecated and were inconsistent across browsers. They still work for backward compatibility and appear in older tutorials, but new code should use event.key or event.code, which are readable strings rather than magic numbers.
What is the keyCode for the Enter key?
The legacy event.keyCode for Enter is 13. The modern equivalents are event.key "Enter" and event.code "Enter" (or "NumpadEnter" for the numpad version, which you can also tell apart using event.location).
How do I detect a key combination like Ctrl+S?
Listen for keydown and check the modifier flag together with the key. For example: if (e.ctrlKey && e.code === "KeyS") { e.preventDefault(); save(); }. The preventDefault stops the browser opening its own save dialog. This tool builds that exact snippet for whatever key you press.
Why does pressing some keys not show anything?
The capture box must have keyboard focus, so click it first. Also, some combinations such as Ctrl+W, Ctrl+T or F5 are intercepted by the browser before the page sees them, so they may never reach the keydown handler.
Do my keystrokes get sent anywhere?
No. The tool reads the keydown event entirely inside your browser using plain JavaScript. Nothing is logged, stored on a server or transmitted over the network, so it is safe to test any key.