File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Trouble with Toolkit.setLockingKeyState behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with Toolkit.setLockingKeyState behavior" Watch "Trouble with Toolkit.setLockingKeyState behavior" New topic

Trouble with Toolkit.setLockingKeyState behavior

Brian McFarland

Joined: Oct 10, 2008
Posts: 8
Hi all,

I'm writing some code that needs to manipulate the NumLock key. I found some examples using the Toolkit class in java, specifically the get and setLockingKeyState methods, which let you find or set the status of the lock keys. These methods show odd behavior for me, however. This is a test snippet of my code, particularly the portion that manipulates the Numlock key:

I haven't been able to find an exact pattern while debugging through this code, but it seems that if my NumLock is turned on when I run the application, then the first line, which turns NumLock on does nothing, which is correct, and the next line will output true, also correct. The line to turn NumLock off will also toggle the key off correctly as well, the weird behavior begins when the next output line still outputs true. After the first test, I will run the code again, the second time through, the first statement still does nothing to the NumLock key, and the first output still outputs true. The third line will turn NumLock back on, despite being false, and the second output remains true. Basically over multiple tests, the third line will toggle NumLock to the opposite of its current state.

The reason I have been unable to find an exact pattern is that sometimes when I first turn my computer on, if NumLock begins in the off state, then the first line of this code will turn toggle the key on and off, like how the third line was, and then every output will show up as "false".

This behavior seems quite bizarre and I have found two threads mentioning this, but I haven't seen any solid answers to the questions. Has anyone encountered this problem before? Thank you very much in advance.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

This issue has shown up last year:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Trouble with Toolkit.setLockingKeyState behavior
It's not a secret anymore!