Saved

Press the star next to a tool's name to save it.

All my tools

Keyboard Key Code Finder (event.key, code, keyCode)

This keyboard key code finder shows what the browser reports for any key you press: event.key, event.code, the legacy keyCode and which values, key location and modifiers, plus a line of JavaScript to test for it.

keyCode is deprecated: use event.key or event.code in new code. Tab is left alone so you can leave the box.

The keys you press are shown on this page only. They are never stored or sent anywhere.

Result

Press a key to see its details.

When you write keyboard shortcuts for a web page you need to know exactly what the browser reports when a key is pressed. This keyboard key code finder shows it live. Click the box, press any key, and you see event.key, event.code, the legacy keyCode and which numbers, the key location, any modifier keys and a ready-made line of JavaScript to test for that key.

How the keyboard key code finder works

Click inside the capture box and press a key. The page listens for the keydown event and reads the fields the browser attaches to it. Nothing needs to be typed or submitted: the result updates on every key press. The box is read-only, so no text is entered. Tab is left alone so you can still move out of the box with the keyboard, and shortcuts that use Ctrl or Command are not blocked, but their details are still shown.

key, code and keyCode: which one to use

Browsers describe a key press in three different ways, and picking the wrong one is the most common cause of shortcuts that work on your keyboard but not on a colleague's.

  • event.key is the character or action the key produces. It takes the modifier keys and the keyboard layout into account, so Shift plus 2 gives a different value on a US and a UK layout. A space is reported as a single space character, special keys have names such as Enter, Escape or ArrowUp, and a key the browser cannot identify is reported as Unidentified. [3]
  • event.code identifies the physical key by its position on the keyboard, whatever the layout. The key in the A position is always KeyA, even on a French keyboard where it is labelled Q. [1]
  • event.keyCode is a number from an older design. It is deprecated, it is not reliable for printable characters and the numbers are mapped differently between browsers. New code should use key or code instead. [2]

A simple rule: use code for positional controls, such as the W, A, S and D keys in a game, and use key for shortcuts that are tied to a character, such as pressing the question mark to open help.

Reading the result table

RowMeaning
keyThe value the key produces, shown in quotes; a space is spelled out.
codeThe physical key, such as KeyA, Digit1, Enter, ShiftLeft, ArrowUp, Space or Numpad0. [1]
keyCode and whichThe legacy numbers. Shown for older code you may be maintaining.
location0 for a standard key, 1 for a left-hand key, 2 for a right-hand key and 3 for the numeric keypad. [4]
modifiersCtrl, Alt, Shift and Meta held at the time of the press.
repeatYes when the key is being held down and the browser is repeating it.
test lineJavaScript you can paste into a keydown handler.

Key location

Some keys exist twice. Pressing the left Shift key and the right Shift key both give the key value Shift, but their codes are ShiftLeft and ShiftRight and their location is 1 and 2. The numeric keypad adds a third kind: the 0 on the keypad has the code Numpad0 and location 3, while the 0 on the number row has the code Digit0 and location 0. Mobile and joystick keys use values 4 and 5. [4]

Examples to try

  • Press the A key, then hold Shift and press it again. The key changes from a to A but the code stays KeyA.
  • Press the left and then the right Shift key and compare the code and location.
  • Press 1 on the number row, then 1 on the keypad. Only the code and location differ.
  • Press the space bar and see how a space is spelled out in the result.
  • Hold a key down and watch repeat switch to yes.

Writing a key handler

A typical handler listens on the document, checks the code or key and then stops the browser doing its usual thing. For example, to react to the Escape key you would test event.key against the string Escape. To react to the physical W key you would test event.code against KeyW. The test line in the results gives you the right comparison for the key you just pressed. Remember to ignore the event while the user is typing in a form field, or your shortcut will fire in the middle of a sentence.

Things to remember

  • Some keys are taken by the browser or the operating system, such as Ctrl+W or the Windows key, and the page may never see them.
  • On phones and tablets the on-screen keyboard often reports less detail, so test shortcuts on a real hardware keyboard.
  • Input method editors, used for languages such as Japanese or Chinese, can change what key reports while a word is being composed.
  • Dead keys, used to type accents, report the value Dead. [3]

The tool runs entirely in your browser. The keys you press are shown on the page and are never stored or sent anywhere.

What next

Use this often? Add Keyboard Key to your Home screen.

Frequently asked questions

What does the keyboard key code finder show?

It shows event.key, event.code, the deprecated keyCode and which values, the key location, any modifier keys, whether the key is repeating and a line of JavaScript to test for it.

What is the difference between event.key and event.code?

key is the character or action the key produces and changes with layout and Shift. code is the physical key position and stays the same on every layout.

Is keyCode still safe to use?

It is deprecated and unreliable for printable characters, so new code should use key or code.

Why does the left Shift key look the same as the right one?

Both report the key value Shift. Compare event.code (ShiftLeft or ShiftRight) or event.location (1 or 2) to tell them apart.

Why can I not see some key combinations?

The browser or operating system takes certain shortcuts before the page can see them, such as Ctrl+W or the Windows key.

Are my key presses recorded?

No. They are shown on the page only and are never stored or sent.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools