Good day to all!
In the application it is necessary to indicate to the user whether the caps lock is pressed on the keyboard.
The code below shows the correct value at startup:

java.awt.Toolkit.getDefaultToolkit().getLockingKeyState(java.awt.event.KeyEvent.VK_CAPS_LOCK); 

But if you press Caps Lock while the application is running, it displays the status that it was at startup.
I need to know the status of Caps Lock at runtime.

The application is written from a GUI to JavaFX.

PS Thanks in advance for your help.

    2 answers 2

    In Application.start we get the status of Caps Lock and write in the status field. Next, in the same Application.start we set an event handler for releasing the keyboard button:

     Scene scene = ...; scene.setOnKeyReleased(event -> { if (event.getCode() == KeyCode.CAPS) { boolean isCapsLockOn = java.awt.Toolkit.getDefaultToolkit().getLockingKeyState( java.awt.event.KeyEvent.VK_CAPS_LOCK)); // Обновляем поле статуса. } }); 

    As @AndrewBystrov correctly noted, if the scene is inactive, then the handler will not work and our status may be invalid. Therefore, let's "hang up" the handler also on the event of receiving the focus:

     Stage stage = ...; stage.focusedProperty().addListener( (observable, oldValue, newValue) -> { if (newValue && !oldValue) { boolean isCapsLockOn = java.awt.Toolkit.getDefaultToolkit().getLockingKeyState( java.awt.event.KeyEvent.VK_CAPS_LOCK); // Обновляем поле статуса. } }); 

      Get the initial status, and then process in keyListener