How can I get Caps Lock status and enable it if it is not already installed?

I need a concrete example of how to enable cap lock if it is turned off.

I know how to switch the key, I used this:

toolkit.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, Boolean.TRUE); 

This will change the state of the key, whether it will be turned on or off. But I want to make sure that it is enabled at the beginning of the application.

(The ultimate goal is that the keyboard LEDs flash in certain sequences, which works better if I have a specific initial state.)

+6
source share
1 answer

You can use getLockingKeyState to check if Caps Lock is installed:

 boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK); 

However, it is not needed - setLockingKeyState does not switch the key state, it sets it. If you pass it true , it will turn on the key state regardless of the initial state:

 Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true); 
+14
source

Source: https://habr.com/ru/post/897410/


All Articles