Punctuation key codes (186–222)
The punctuation keys send event.code names such as "Semicolon" and "Slash" and keyCode 186 to 222, but the numbers change with the browser and the layout.
Every value at a glance
| Key | event.code | event.key | keyCode | keyCode in Firefox | event.location |
|---|---|---|---|---|---|
| Semicolon | Semicolon | ; | 186 | 59 | 0 (Standard) |
| Equal | Equal | = | 187 | 61 | 0 (Standard) |
| Comma | Comma | , | 188 | 0 (Standard) | |
| Minus | Minus | - | 189 | 173 | 0 (Standard) |
| Period | Period | . | 190 | 0 (Standard) | |
| Slash | Slash | / | 191 | 0 (Standard) | |
| Backquote | Backquote | ` | 192 | 0 (Standard) | |
| Left Bracket | BracketLeft | [ | 219 | 0 (Standard) | |
| Backslash | Backslash | \ | 220 | 0 (Standard) | |
| Right Bracket | BracketRight | ] | 221 | 0 (Standard) | |
| Quote | Quote | ' | 222 | 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 punctuation key
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Semicolon
document.addEventListener('keydown', (event) => {
if (event.key === ';') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'Semicolon') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 186 || event.keyCode === 59) {
// Your code here
}
});Which value to use
For a character, such as “/” to open a search box or “?” for help, check event.key: event.key === '/'. It is what the user’s layout types, the same thing the user sees on the key.
For a place on the keyboard, check event.code, such as BracketLeft and BracketRight for “previous” and “next”. The code stays the same on every layout, even where the key types a letter.
Avoid keyCode here more than anywhere. These are the keys where browsers and layouts disagree.
What trips people up
Firefox sends 59, 61 and 173
For the ; = and - keys, Chrome, Edge and Safari send 186, 187 and 189. Firefox sends 59, 61 and 173. Old code that checks 187 to zoom in does nothing in Firefox. Worse, 173 is the mute key in Chrome. If you must keep keyCode, check both numbers, but for - that also catches the mute key in Chrome; better, check event.key.
The number changes on another layout
On a German QWERTZ keyboard, the key right of L types “ö”, and its keyCode is 192 in Chrome on Windows, not 186. On a Japanese keyboard the same key types “;” but sends 187. Match characters by event.key and places by event.code.
A “/” or “?” shortcut doesn’t fire
On many layouts these need Shift: “/” is Shift + 7 on a German keyboard. A check for event.code === 'Slash' or keyCode 191 misses them. Check event.key === '/' or event.key === '?', and don’t add event.shiftKey for “?”: Shift is part of how the user types it.
The key sends “Dead”
Many European layouts have dead keys: they wait for the next key to add an accent. German ^ and ´, French ^ and the ’ and keys on US International sendevent.key"Dead", and the next key gets the accented letter. A console toggle on the key never sees “" on those layouts, so match such toggles by event.code (Backquote`).
An extra key next to Left Shift
European (ISO) keyboards have one more key between Left Shift and Z. Its code is IntlBackslash, and it isn’t in the table above. It types “<” on a German keyboard and “\” on a UK one. Its keyCode depends on the layout (often 226 on Windows), so match it by event.key or event.code.