How do I check if the caps lock key is pressed?

拟墨画扇 提交于 2019-12-17 16:57:09

问题


Alright, before this gets flagged as a possible duplicate, I've already tried the following code:

Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)

And it is always returning false for me [see below]. Could someone confirm if this is supposed to be working, and I'm misusing it, or if it's known to be broken? If it is in fact broken, does anyone have a better method to use?

EDIT:

Alright, just found out something more. It appears to just return what it was at the begining of my programs launch. If I start the program with it on, it says its on, and vice versa. Here's my code:

while (true) {
    boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(
        KeyEvent.VK_CAPS_LOCK);
    System.out.println("Caps lock is now: " + (isOn ? "ON" : "off"));
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
}

And that's just always printing out whatever it started as

(ex. if I start with caps lock on, even if I toggle it off right after, it prints:

Caps lock is now: ON

Caps lock is now: ON

Caps lock is now: ON

Caps lock is now: ON

etc., if I start with it off, it will print off no matter what)


回答1:


Poking around, I think getLockingKeyState() might be broken.

You could try KeyboardUtils, but it looks like that means you have to carry JNA overhead.




回答2:


Looks like this was always broken or at least since Java 1.3 (see Bug 4414164).

Not sure for other platforms, but for Windows I can say this: State change of Caps Lock can be detected, but only if your awt client has the focus. However, there is this workaround which works for me:

boolean isCapsLockOn() {
    java.awt.Robot robot = new java.awt.Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    return Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);  
}

Contraint: Your awt app must have the focus before calling isCapsLockOn.

Note: Your robot might press any other key which is not harmful to your app. Might depend on your use case.




回答3:


public void checkOnOff() {
    Thread th = new Thread() {
        public void run() {
            for (;;) {
                if (Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)) {
                    jLabel4.setForeground(Color.red);
                    jLabel4.setText("CAPSLOCK is ON");
                } else {
                    jLabel4.setText(null);
                }
                try {
                    sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }                    
            }
        }
    };th.start();
}


来源:https://stackoverflow.com/questions/12020835/how-do-i-check-if-the-caps-lock-key-is-pressed

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