4 ms·
> Doesn't this only detect when you press caps lock and turn it on? That's the result I got from testing it out in the browser console. No, if CapsLock was set
by rezonant 3y ago
> Doesn't this only detect when you press caps lock and turn it on? That's the result I got from testing it out in the browser console.
No, if CapsLock was set to on when you pressed the key, getModifierState('CapsLock') returns true. It is true that this doesn't let you query the caps lock status directly at will, instead you have to wait for a keyboard event. But it is not merely telling you that Caps Lock itself was pressed, rather that a key was pressed while CapsLock was enabled.
This is exactly how it's working for me as I test it on this textarea.
So if you wanted to add one of those "Hey, Caps Lock is on" warnings to a password field, you could do so, you'd just have to wait for the first key to be pressed.