How can I get the keyboard Caps Lock state in javafx

烈酒焚心 提交于 2019-12-24 17:01:34

问题


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


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/26529180/how-can-i-get-the-keyboard-caps-lock-state-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!