Java工具包在获取更新的大写锁定状态时遇到问题。

huangapple 未分类评论43阅读模式
英文:

Java Tooklit having problems grabbing updated caps lock status

问题

使用Java的工具包(java.awt.Toolkit和import java.awt.event.KeyEvent)来检查大写锁定状态。然而,我无法更新大写锁定状态。它似乎只在开始时获取一次大写锁定实例,并保持在那个位置。所以例如,在这段代码中:

for(int i = 0; i < 10; i++) {
    boolean temp = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
    System.out.print(temp + " ,");
    TimeUnit.SECONDS.sleep(1);
}

理论上它应该每隔1秒钟检查10次。例如,如果在前5秒钟大写锁定打开,在后5秒钟关闭,那么控制台应该输出:

true, true, true, true, true, false, false, false, false, false, 

然而,我的代码只会在进入for循环时获取大写锁定键的状态,并保持那种状态,无论我后来是打开还是关闭它。

例如,如果我在前5秒钟保持大写锁定打开,在后5秒钟关闭,就像上面的情况,它会输出:

true, true, true, true, true, true, true, true, true, true, 

或者如果我在前5秒钟保持大写锁定关闭,然后在后5秒钟打开,它会输出:

false, false, false, false, false, false, false, false, false, false, 

有人知道如何修复这个问题,使其能够动态更新按键状态吗?

谢谢!

英文:

I am using Java's toolkit package (java.awt.Toolkit and import java.awt.event.KeyEvent) to check the caps lock status. I however, cannot update the caps lock status. It seems to grab the caps lock instance only once, and stays in that position. So for example, in this code:

for(int i = 0; i &lt; 10; i++) {
    boolean temp = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
    System.out.print(temp + &quot; ,&quot;);
    TimeUnit.SECONDS.sleep(1);
}

It should theoretically check 10 times, every 1 second. So for example, if my caps lock is on during the first 5 seconds, and off during the last 5 seconds, then the console should output

true, true, true, true, true, false, false, false, false, false, 

However, what my code does, is just grab what my caps lock key is like when it first enters the for loop, and stay that way, regaurdless of wether I turn it off or on afterwards.

So for example, if I keep my caps lock on for the first 5 seconds, and off the last 5 secs, like above, it will output

true, true, true, true, true, true, true, true, true, true, 

or if I keep my caps lock key off the first 5 seconds then on the last 5 secs, it will output

false, false, false, false, false, false, false, false, false, false,
Anyone know how to fix this so it dynamically updates the keypress status?

Thanks!

huangapple
  • 本文由 发表于 2020年7月25日 10:30:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/63083704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定