Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getLockingKeyState and setLockingKeyState unreliable?  RSS feed

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, well, first off, I know these only seem to work under Windows, and throw UnsupportedOperationExceptions in Linux, Solaris, etc.

However, are these known to be unreliable even in Windows?

I'm using J2SDK 1.5.0 update 5.

I'm working on a program that, when the window focuses on a particular screen, I get the states of scroll lock, num lock, and caps lock with getLockingKeyState and store the info. Then I use setLockingKeyState to set those LEDs to different values (not all the time, though).

When the window loses focus, I use setLockingKeyState to restore the original values I'd stored before. A subsequent focus on the application window will call the storage function again (in case the user changed the keys' states while not in my application).

Sounds all well and good, but it doesn't seem to work reliably. I've got println statements to confirm what I'm getting from the getLockingKeyState calls. I've noticed that sometimes I'll get false positives and false negatives. Generally it'll work right on the first entry into the app window, and switch back out... after that, it tends not to work at all.

That is, I'll see that NumLock is lit, but getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK) will return false. Or I'll try to getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false) while the NumLock key is lit, and it will stay lit.

Occasionally there's a hiccup where it might work once or twice again, but in general, I'll only seem to get one initial reliable use out of it.


Is this known to be an issue? I thought maybe adding Toolkit.sync() might help, but it didn't seem to make a difference.

Here's the code that's being called (it's a bit sloppy for now).
 
Ed Tidwell
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post a stack trace of the error your getting?

Thanks,
Ed
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the thing . . I'm not getting any errors, just incorrect behavior.

The main program is basically a remote computer administration tool. To wit, you click on a remote machine, and a window pops up showing you the desktop of the remote system. There's also a status bar that shows the text NUM, SCRL, and CAPS to tell you what the remote system thinks the lock-keys are set to.

My code was an extra method that would take those values and use setLockingKeyState to make the LEDs of the keyboard you're sitting at match what the remote system thinks is happening. That is, if your own keyboard only has NUM lock on, and the remote system has NUM and SCRL on, then your keyboard's LEDs should switch to having but NUM and SCRL on.

I'm using setLockingKeyState to do this. However, when I use that method, it sometimes causes a keypress event to occur. This keypress gets captured by the main part of the program and transmitted to the remote machine sometimes.

Doesn't seem to happen consistently, though.

Furthermore, I've used JBuilder to step through the code, and I've seen, say, the following line:
Toolkit.getDefaultToolkit.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false) result in an already-off CAPS lock LED get turned on!

Again, it doesn't happen consistently.


As a workaround, if there's something I can add to the code so that I can forcibly clear-out any and all keypress events after setLockingKeyState, I'd do that as a workaround.

Haven't found anything quite suitable yet. I haven't worked a lot with awt, keyEvents, and the like, so this is a bit new to me. I'm more accustomed to dealing with Swing components and events with GUI items.
[ September 27, 2005: Message edited by: Joe Vahabzadeh ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!