如果尚未获得Caps Lock状态,并将其设置为on,我该怎么办? [英] How can I get the Caps Lock state, and set it to on, if it isn't already?
问题描述
我想了解一个关于如何关闭的大写锁定的具体示例。
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.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, Boolean.TRUE);
这将改变密钥的状态,无论密钥是打开还是关闭。
但是我想确保它在应用程序开始时打开。
That will change the state of the key whether it is on or off. But I want to make sure it is on at the beginning of the application.
(最终目标是让键盘LED以某些顺序闪烁,如果我有一定的起始状态,效果会更好。)
(The final goal is having the keyboard LEDs flash in certain sequences, which works better if I have a certain starting state.)
推荐答案
你可以使用 getLockingKeyState
检查是否Caps Lock目前已设置:
You can use getLockingKeyState
to check if Caps Lock is currently set:
boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
然而,这是不必要的 - setLockingKeyState
不切换键的状态,它设置它。如果您传递 true
,无论原始状态如何,它都会将关键状态设置为开启:
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);
这篇关于如果尚未获得Caps Lock状态,并将其设置为on,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!