Is there any way to turn on/off Num, Caps and Scroll Lock lights? It's just for fun and my idea is to do something like disco.
3 Answers
Try
Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);
to set Caps lock On.
Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false);
to set Caps lock Off.
and
boolean isCapsLockOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
to get current status.
use KeyEvent.VK_NUM_LOCK
for num lock.
I have tested this on Windows. I'm not sure about other OSs but I would think it would work.

- 107,317
- 23
- 199
- 210
-
just to be clear, this actually changes their states and not just turns the lights on/off, correct? – Daniel DiPaolo May 03 '11 at 19:07
-
@Daniel on my system it does change the state with in-turn turns the light on/off. – Bala R May 03 '11 at 19:09
-
It also changes the states, yes. But that isn't a problem. Here the outcome: http://non.dagrevis.lv/junk/java/Disco.jar . =] – daGrevis May 03 '11 at 19:11
Probably through JNI and a c method. Check some discussions here:

- 40,203
- 9
- 86
- 108
I think you cannot set Caps/Scroll lock's lights independently from their own activation, in a direct way.
You could only achieve such behaviour by programming a keyboard driver of your own (or eventually browsing the web for a driver that might already have been developed to achieve such a behavior), but that is not recommended, and goes way beyond the boundaries of java.
Also, this question is a similar duplicate of Way to turn on keyboard's caps-lock light without actually turning on caps-lock? , even though in this other question that behavior is intended in C#.
However, the issue, impossibility, and the way around it, are the same.

- 1
- 1

- 5,029
- 2
- 41
- 41