Detect Caps Lock in JavaScript

Kate Rose Mosley has a method for detecting caps lock in JavaScript. It´s important to listen to keydown and keyup because browsers behave differently.

addEventListener('keydown', detectCapsLock)
addEventListener('keyup', detectCapsLock)

function detectCapsLock(e) {
if (e.getModifierState('CapsLock')) {
console.log('caps lock is on');
} else {
console.log('caps lock is off');
}
}
Comments