How to detect Caps Lock
document.addEventListener('keydown', (event) => {
if (event.key === "CapsLock") {
// Caps Lock was pressed
}
});
Use event.key to react to what the key means (it follows the keyboard layout) and event.code for its physical position, for example WASD in games. keyCode and which are deprecated: they still work, but their values differ between browsers and layouts.
How browsers treat Caps Lock
To warn users that Caps Lock is on in a password field, check event.getModifierState("CapsLock") on any key event — it reports the lock state, not just this key.
In React
<input onKeyDown={(e) => { if (e.key === "CapsLock") { /* … */ } }} />