bug in caps lock detection code
Hi all,
I am trying to detect caps lock key's on/off state using Toolkit's getLockingKeyState() method. Whenever I call this in a key or focus listener's method such as in the following it does not work correctly. It always return true:
Code:
textField_1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
lblCapsLock.setVisible(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
}
});
What I am trying to achieve is to notify the user via enabling a label when the key is on.
Re: bug in caps lock detection code
Code:
public static void main(String args[]) {
boolean lock = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
System.out.println(lock);
}
Running the above gives the correct result when run on Win 7 (via Eclipse).
Stick some debugging in your focusGained() to print out the value of the lock.
Re: bug in caps lock detection code
In Windows 7 & XP it is working perfectly, but our main development environment is Red Hat Enterprise Linux (RHEL 6.2). So this code is not working correctly on that. In debug mode in Eclipse, it is working correctly(since halting the program gives some timing to aquire caps lock's state) but in release mode it's not. I have found a workaround using Thread.sleep but it is not a good idea to use in our production code. So is there anyone who come across this problem in RHEL ?
Re: bug in caps lock detection code