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.
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);
}
}
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…