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.