Is it possible to check the status of Scroll Lock, Num Lock and Caps Lock on page load of a web page? I've found ways to check after a keypress using JavaScript, but that's not what I'm asking.
Asked
Active
Viewed 5,590 times
2 Answers
10
IN 2019, this is now possible:
var x = event.getModifierState("ScrollLock");
Source: https://www.w3schools.com/jsref/event_mouse_getmodifierstate.asp

thebiss
- 156
- 1
- 4
-
1If you are coming here in 2019, please make sure to use this answer! Also, consider watching for `keydown` or `keyup` instead of `keypress` to see when people press modifier keys when already on your app/site. `keypress` doesn't capture modifier keys in my tests, but the others do. – Kevin Peno Sep 16 '19 at 02:59
-
Sweet! Glad I asked this years ago! Finally being put to good use today! https://www.w3schools.com/Jsref/event_key_getmodifierstate.asp – JustBeingHelpful Jun 25 '21 at 20:48
9
No, you can't get system state from javascript. You will need them to type something and then analyze the input. Probably not what you wanted to hear =/

mrtsherman
- 39,342
- 23
- 87
- 111
-
-
Check here:- http://dougalmatthews.com/articles/2008/jul/2/javascript-detecting-caps-lock/ – Bajrang Jan 30 '12 at 06:25
-
2@J.J. - yeah, I'm sure. Browsers are pretty heavily sandboxed, so being able to detect the states of hardware peripherals is out. How about checking if you have a webcam? Or a mic? Or how many buttons your mouse has. We can play all we want with events once the user triggers an event in the window, but we can't go out and get them. – mrtsherman Jan 30 '12 at 06:25
-
-
-
@J.J. - yah, I have that function implemented in my code already. It's just annoying that they have to type something first and then it turns on. Most people wouldn't care since you only care in the password field, it's not a big deal since you'll be typing already in the login field first. However, we have password fields within our lab software without a login field within the application (when they're already logged in). It's used for auditing purposes. – JustBeingHelpful Jan 30 '12 at 06:29
-
the function has one flaw though.. it doesn't check whether the keypress came from the [DELETE], [BACKSPACE] or [CAPS LOCK] keys. You have to explicitly check those. There might be others that I'm not thinking of. – JustBeingHelpful Jan 30 '12 at 22:35
-
"From javascript" is too big word, because javascript is also present in other contexts such as runtimes (e.g. Adobe AIR ) and scripts. And Adobe AIR as an example - allows to achieve this. – luke1985 Jan 16 '14 at 21:57