問題描述
我的問題是 Toolkit.getDefaultToolkit().getLockingKeyState(...)
永遠不會更新.它在我第一次查詢時正確報告,然后當我使用鍵盤更改狀態時,更改永遠不會反映.
I have the problem that Toolkit.getDefaultToolkit().getLockingKeyState(...)
never updates. It reports correctly the first time I query it, then when I change the state using keyboard, the change is never reflected.
這是設計使然(API 文檔中似乎并非如此)、錯誤還是我的代碼有問題?
Is this by design (doesn't seem so in the API doc), a bug, or is there something with my code?
這里有一個簡短的、獨立的示例來演示該問題:
Here's a short, self-contained example to demonstrate the issue:
public class LockingStateIssue {
public static void main(String[] args) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override public void run() {
if (Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK)) {
System.out.print("*");
} else {
System.out.print(".");
}
}
}, 0, 200);
}
}
在我的 Windows 機器上使用 Java 1.7.0_45 運行時,它會打印 .......
或 *********
取決于 num lock 鍵的初始狀態,但絕不會像我期望的那樣混合 ..**.**
在切換按鈕時.
When run, on my Windows machine, using Java 1.7.0_45, it prints either .......
or *********
depending on the initial state of the num lock key, but never a mix like ..**.**
like I expect it to, when toggling the button.
推薦答案
沒有 可見的 Java 容器 并且在窗口中也有焦點,KeyLoggers 在 Java 中被阻止,
there isn't a correct way in plain Java without visible Java container and with focus in windows too, KeyLoggers are blocked in Java,
(可能不是主要問題,但也不會打印任何內容)來自 util.Timer 的循環已超出 EDT,更多內容請參見 Swing 中的并發性,Toolkit 來自 AWT 包,EDT 問題也適用于大多數 AWT 包
(could not be main issue, but nothing will be printed too) loop from util.Timer is out of EDT, more in Concurency in Swing, Toolkit is from AWT package, EDT issue is valid for most of AWT packages too
為我工作(在滿足前兩點的情況下,要求..),用于測試目的,在此程序執行期間嘗試將焦點移至 Windows 操作系統中的另一個活動窗口
work for me (in the case that previous two points, requirements are met..), for testing purpose to try move focus to another active window in Windows OS durring this program execution
代碼
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
public class ToolkitAndNumLock {
private javax.swing.Timer timer = null;
private JFrame frame = new JFrame();
public ToolkitAndNumLock() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
start();
//uncomment Toolkit.getXxx listening a KeyEvents, you can (start();) block SwingTimer
//Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
}
AWTEventListener listener = new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) event;
if (ke.getID() == KeyEvent.KEY_PRESSED) {
if (ke.getKeyCode() == KeyEvent.VK_CAPS_LOCK) {
System.out.println("CapsLock Pressed");
}
if (ke.getKeyCode() == KeyEvent.VK_SCROLL_LOCK) {
System.out.println("ScrollLock Pressed");
}
if (ke.getKeyCode() == KeyEvent.VK_NUM_LOCK) {
System.out.println("NumLock Pressed");
}
}
}
}
};
private void start() {
timer = new javax.swing.Timer(2500, updateCol());
timer.setRepeats(true);
timer.start();
}
public Action updateCol() {
return new AbstractAction("text load action") {
private static final long serialVersionUID = 1L;
private Boolean bol = true;
@Override
public void actionPerformed(ActionEvent e) {
if (Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK)) {
System.out.println("true");
} else {
System.out.println("false");
}
if (bol) {
Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, true);
} else {
Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false);
}
bol = !bol;
}
};
}
public static void main(String args[]) {
Runnable runner = new Runnable() {
@Override
public void run() {
new ToolkitAndNumLock();
}
};
EventQueue.invokeLater(runner);
}
}
這篇關于Toolkit.getDefaultToolkit().getLockingKeyState(...) 在程序執行期間從不更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!