Reegan Miranda
Reegan Miranda

Reputation: 2949

How can I get the keyboard Caps Lock state in javafx

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

Upvotes: 1

Views: 1902

Answers (2)

jewelsea
jewelsea

Reputation: 159416

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.

Upvotes: 2

Robert Martin
Robert Martin

Reputation: 1605

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);
    }

Upvotes: 3

Related Questions