java 如何获得 Caps Lock 状态并将其设置为开启(如果尚未开启)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7435221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:57:12  来源:igfitidea点击:

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

javatogglecapslock

提问by Mike George

I would like a specific example on how to turn caps lock on ifit 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.

这将改变键的状态,无论它是打开还是关闭。但我想确保它在应用程序开始时就打开了。

(The final goal is having the keyboard LEDs flash in certain sequences, which works better if I have a certain starting state.)

(最终目标是让键盘 LED 以特定顺序闪烁,如果我有特定的启动状态,效果会更好。)

回答by Michael Mrozek

You can use getLockingKeyStateto check if Caps Lock is currently set:

您可以使用getLockingKeyState检查当前是否设置了 Caps Lock:

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

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

但是,这是不必要的——setLockingKeyState不会切换键的状态,而是设置它。如果你通过它,true它会将关键状态设置为 on,而不管原始状态如何:

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