How can I get the Caps Lock state, and set it to on, if it isn't already?

前端 未结 1 1445
Happy的楠姐
Happy的楠姐 2020-12-07 01:30

I would like a specific example on how to turn caps lock on if it is off.

I know how to toggle the key, I have been using this:

toolkit.setL         


        
1条回答
  •  攒了一身酷
    2020-12-07 01:43

    You can use getLockingKeyState to check if Caps Lock is currently set:

    boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
    

    However, it's unnecessary -- setLockingKeyState doesn't toggle the state of the key, it sets it. If you pass it true it will set the key state to on regardless of the original state:

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

    0 讨论(0)
提交回复
热议问题