1

In my project, how to keyboard caps lock on state. i have refer this question How can I get the Caps Lock state, and set it to on, if it isn't already?. but i am get solution in javafx. please give me solution.I am also ref for this site https://community.oracle.com/thread/2415027?tstart=0

Community
  • 1
  • 1
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55

2 Answers2

3

You will want this import:

import java.awt.Toolkit;

If you are wanting it on no matter what, just turn it on with:

Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);

If you want to check first if it is off, then turn it on:

if (!Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)) {
        Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);
    }

Lastly, if you want to toggle between the two states:

if (Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)) {
        Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false);
    } else {
        Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);
    }
Robert Martin
  • 1,585
  • 1
  • 13
  • 20
2

I don't think it is possible to query the capslock/numlock state directly in JavaFX 8. Robert's solution uses the AWT Toolkit, which is not JavaFX, but should work for you. You might want to create a feature request in the JavaFX issue tracker for locking key state tracking.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Note an enhancement request has existed since 2012: https://bugs.openjdk.java.net/browse/JDK-8090882 – Slaw Nov 10 '20 at 20:33