Caps Lock key code
Caps Lock sends event.key "CapsLock", event.code "CapsLock" and keyCode 20.
Every value at a glance
| Key | event.code | event.key | keyCode | event.location |
|---|---|---|---|---|
| Caps Lock | CapsLock | CapsLock | 20 | 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 Caps Lock
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Caps Lock
document.addEventListener('keydown', (event) => {
if (event.key === 'CapsLock') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'CapsLock') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 20) {
// Your code here
}
});Which value to use
To know whether Caps Lock is on, don’t watch the key. Ask any key or mouse event: event.getModifierState('CapsLock') is true while it’s on. That works even if Caps Lock was switched on before the page loaded.
To react to the key itself, check event.key === 'CapsLock' (event.code is the same). keyCode is 20, but it is deprecated.
Caps Lock changes event.key for letters (“A” instead of “a”) but not event.code or keyCode.
What trips people up
My “Caps Lock is on” warning is wrong
Counting Caps Lock presses goes wrong as soon as it was on before the page opened. Read the state from each event on the password field instead:
function checkCaps(event) {
warning.hidden = !event.getModifierState('CapsLock');
}
password.addEventListener('keydown', checkCaps);
password.addEventListener('keyup', checkCaps);
Hide the warning on blur. The state is only known once the user presses a key or moves the mouse; there’s no way to ask on page load.
On a Mac, Caps Lock sends only one event
On Windows and Linux, Caps Lock sends keydown and keyup like any key. On a Mac, Chrome and Safari send only a keydown when it turns on and only a keyup when it turns off. Firefox on a Mac sends only a keydown each time. That’s why the warning above listens to both.
The state on Caps Lock’s own event is old
During the Caps Lock event itself, browsers differ on whether getModifierState already shows the new state. Check it again on the next key, or on keyup.
A quick tap doesn’t switch it on (Mac)
macOS ignores a very short tap on Caps Lock, so a brushed key doesn’t turn it on. Your page may see the event while the light stays off. Trust getModifierState, not the event count.
Phones never say Caps Lock is on
On-screen keyboards have their own caps lock (double-tap Shift). It doesn’t send Caps Lock events, and getModifierState('CapsLock') stays false. Only a hardware keyboard reports it.
Caps Lock in games and shortcuts
Don’t use Caps Lock as a hold-down key: on a Mac you won’t get a keyup while it’s pressed. Some people also remap it to Ctrl or Esc, and then the page gets that key instead. Safari on a Mac already shows its own Caps Lock symbol in password fields, so your warning may appear twice there.