Number key codes (0–9)
The number keys above the letters send event.code "Digit0" to "Digit9", event.key "0" to "9" on a US layout and keyCode 48 to 57.
Every value at a glance
| Key | event.code | event.key | keyCode | event.location |
|---|---|---|---|---|
| 0 | Digit0 | 0 | 48 | 0 (Standard) |
| 1 | Digit1 | 1 | 49 | 0 (Standard) |
| 2 | Digit2 | 2 | 50 | 0 (Standard) |
| 3 | Digit3 | 3 | 51 | 0 (Standard) |
| 4 | Digit4 | 4 | 52 | 0 (Standard) |
| 5 | Digit5 | 5 | 53 | 0 (Standard) |
| 6 | Digit6 | 6 | 54 | 0 (Standard) |
| 7 | Digit7 | 7 | 55 | 0 (Standard) |
| 8 | Digit8 | 8 | 56 | 0 (Standard) |
| 9 | Digit9 | 9 | 57 | 0 (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 number key
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to 0
document.addEventListener('keydown', (event) => {
if (event.key === '0') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'Digit0') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 48) {
// Your code here
}
});Which value to use
To react to a typed digit, check event.key: /^[0-9]$/.test(event.key). That accepts the top row and the number pad (with Num Lock on), on every layout that types the digit.
Use event.code (Digit1) when the place matters, such as “press 1 to 5” to pick a slot in a game. The code is the same on every layout, even where the key types something else without Shift.
keyCode 48 to 57 is the top row only. The number pad sends 96 to 105 for the same digits, so a check for 48 to 57 misses it.
What trips people up
Digits don’t arrive from a French keyboard
On a French AZERTY keyboard in Windows, the top row types & é “ ’ ( - è _ ç à without Shift. Digits need Shift. So pressing Digit2 sends event.key “é”, while keyCode still says 50 in Chrome and Firefox on Windows. For a typed number, read the field’s value or event.key. For “press 1” shortcuts, check event.code, so French users don’t have to hold Shift.
Shift+2 isn’t “@” everywhere
The symbols on Shift + digit depend on the layout. Shift + 2 is “@” on a US keyboard and " on UK and German ones. Shift + 3 is “#” on a US keyboard, “£” on a UK one and “§” on a German one. To catch a symbol, check event.key === '@', not event.shiftKey with Digit2. Old code that turns keyCode 50 plus Shift into “@” types the wrong thing for these users.
The number pad sends other codes
A number pad digit has its own code (Numpad1) and keyCode (97), but the same event.key “1” while Num Lock is on. With Num Lock off it sends “End” instead. Many laptops have no number pad at all, so never require one: check event.key and both rows work.
Typing a number in a field fires your shortcut
Single-key shortcuts such as 1 to 9 also fire while the user types into a field. Skip keys aimed at a text field:
if (event.target.closest('input, textarea, select, [contenteditable]')) return;