Letter key codes (A–Z)

Letter keys send event.code "KeyA" to "KeyZ", event.key "a" to "z" on a US layout ("A" to "Z" with Shift or Caps Lock) and keyCode 65 to 90.

Every value at a glance

Values for Letters A–Z
Keyevent.codeevent.keykeyCodeevent.location
AKeyAa650 (Standard)
BKeyBb660 (Standard)
CKeyCc670 (Standard)
DKeyDd680 (Standard)
EKeyEe690 (Standard)
FKeyFf700 (Standard)
GKeyGg710 (Standard)
HKeyHh720 (Standard)
IKeyIi730 (Standard)
JKeyJj740 (Standard)
KKeyKk750 (Standard)
LKeyLl760 (Standard)
MKeyMm770 (Standard)
NKeyNn780 (Standard)
OKeyOo790 (Standard)
PKeyPp800 (Standard)
QKeyQq810 (Standard)
RKeyRr820 (Standard)
SKeySs830 (Standard)
TKeyTt840 (Standard)
UKeyUu850 (Standard)
VKeyVv860 (Standard)
WKeyWw870 (Standard)
XKeyXx880 (Standard)
YKeyYy890 (Standard)
ZKeyZz900 (Standard)

keyCode as Chrome and Edge send it on Windows. The Mac column (Chrome and Safari on a Mac) and the Firefox column show only numbers that differ; some keys also differ by system, as the text below says. event.which is always the same number as keyCode. event.key is for a US layout without Shift.

Press it here

Click here, then press a letter key

Using only a keyboard? Press Esc, then Tab, to leave the key box.

Code to react to A

Recommended: match event.key
document.addEventListener('keydown', (event) => {
  if (event.key === 'a') {
    // Your code here
  }
});
Match the key's place: event.code
document.addEventListener('keydown', (event) => {
  if (event.code === 'KeyA') {
    // Your code here
  }
});
Old code that still uses keyCode (deprecated)
document.addEventListener('keydown', (event) => {
  if (event.keyCode === 65) {
    // Your code here
  }
});

Which value to use

It depends on whether the letter matters or the place on the keyboard.

For typed letters and shortcuts named after a letter, such as Ctrl + Z for undo, check event.key. It follows the user’s layout, so the shortcut works on the key that shows a Z.

For keys picked by their place, such as WASD in a game, check event.code. KeyW is the key above S on every layout, even on a French AZERTY keyboard where it types “z”.

keyCode is the number of the uppercase letter, 65 for A to 90 for Z, with or without Shift. On Latin layouts it usually follows the letter typed: on AZERTY, the key with code KeyQ types “a” and sends 65. On Russian, Greek and other non-Latin layouts it is usually the US letter in that place.

What trips people up

event.key is “A” instead of “a”

Shift and Caps Lock change event.key. Compare in lowercase, event.key.toLowerCase() === 'a', and check event.shiftKey on its own if Shift matters. This also helps on a Mac, where Cmd + Shift + Z can arrive as a lowercase “z”.

WASD controls don’t work on a French keyboard

If you check event.key === 'w', AZERTY players must reach for the W at the bottom left. Check event.code (KeyW, KeyA, KeyS, KeyD) so the keys stay in the same place. To show the right letters in your help text, Chrome and Edge can tell you what a code types: navigator.keyboard.getLayoutMap().

Ctrl+Z lands on the wrong key on German or French keyboards

German QWERTZ swaps Z and Y, and on AZERTY the Z sits where W is on a US keyboard. A shortcut checked with event.code === 'KeyZ' makes German users press the key labelled Y. Match letter shortcuts with event.key.

Shortcuts stop working on a Russian or Greek layout

There event.key is the letter of that alphabet: Ctrl + Z on a Russian layout sends “я”. Fall back to the place when the key isn’t a Latin letter:

const letter = /^[a-z]$/i.test(event.key)
  ? event.key.toLowerCase()
  : event.code.startsWith('Key') ? event.code.slice(3).toLowerCase() : '';

Alt+letter types a character on a Mac

Option + S types “ß”, so event.key is “ß”, not “s”. Option + E is a dead key: event.key is “Dead” and the next letter gets the accent. Match Alt shortcuts with event.code and event.altKey. On Windows, layouts with AltGr do the same with Ctrl + Alt: Ctrl + Alt + E types € on a German keyboard, so avoid Ctrl + Alt + letter shortcuts.