What are JavaScript key codes?

When you press a key, the browser sends a keyboard event that JavaScript reads in handlers such as onkeydown. The event names the key in more than one way:

  • key is what the key types or does, such as a, A, !, Enter or ArrowUp. It follows the keyboard layout and Shift, so use it for text and shortcuts.
  • code is the physical key, such as KeyA or Digit1, named after its place on a US keyboard whatever the layout. Use it for game controls like WASD.
  • keyCode is an older number, such as 13 for Enter. It's deprecated, but plenty of existing code still uses it.

On a French AZERTY keyboard, for example, the key where a US keyboard has Q types "a", so key is a and code is KeyQ.

Tool description

Press any key while the page is open, and the tool shows its Key and Code, whether the Ctrl, Shift, Alt and Meta keys were held down, and the deprecated keyCode. Each value has a copy button. Tick Prevent default to stop the browser's own action for a key, such as F5 reloading the page or Tab moving the focus, so you can test those keys too.

Features

  • The key, code and keyCode of every key you press
  • Which modifier keys were held: Ctrl, Shift, Alt (Option on a Mac) and Meta (the Windows key or Command)
  • A Prevent default option for testing keys the browser normally acts on

Tips

  • The browser and the operating system keep some shortcuts for themselves, such as closing the tab or switching windows, and Prevent default can't stop those.
  • Shift changes key but not code: on a US layout, Shift with 1 gives the key ! and the code Digit1.