Offering new line in User32.java

47 views
Skip to first unread message

Bob Whiteside

unread,
Dec 14, 2011, 5:10:50 PM12/14/11
to Java Native Access
Hi All,

I needed "GetKeyState", which I think is not currently defined in
User32.

Here's the code I offer for addition to some future version (I hope
this helps):

/**
* @param vKey
* Specifies one of 256 possible virtual-key codes.
* @return
* If the high-order bit is 1, then the key is down; otherwise, it
* is up. If the low-order bit is 1, then the key is toggled.
* "Toggled" applies to keys like CAPS LOCK and NUMLOCK that can
* be toggled on and off by a key press.
*/
short GetKeyState(int vKey);

I looked a little to see if there was a way I could include a unit
test, although I dunno how one would really do that. I suppose I
could have written one that called the new method, then asserted that
I didn't crash. In any case, I only did some interactive testing.

Cheers,
--Bob

Timothy Wall

unread,
Dec 14, 2011, 5:15:30 PM12/14/11
to jna-...@googlegroups.com
java.awt.Robot could do the trick, or simply check the state of a key that is *not* pressed.

Timothy Wall

unread,
Dec 14, 2011, 5:16:25 PM12/14/11
to jna-...@googlegroups.com
BTW, it's very easy to fork a git repo and push the patch (and much less work for someone else to then pull/test/merge your patch).

On Dec 14, 2011, at 5:10 PM, Bob Whiteside wrote:

Bob Whiteside

unread,
Dec 14, 2011, 6:31:13 PM12/14/11
to jna-...@googlegroups.com
@timothy,
The reason I needed this is that the java.awt.Toolkit implementation of getLockingKeyState  method is broken on Windows. 

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6446890

The bug report has been there for a decade, and shows no sign of being fixed.  The problem is that their implementation caches the state of the toggle, then updates that cache as it sees key events.  This fails when the app doesn't have the focus, so a background app cannot get the correct value. 

My (admittedly weird) requirement is that a background app can watch the state of the NUMLOCK light.

Cheers,
--Bob

Bob Whiteside

unread,
Dec 14, 2011, 6:31:45 PM12/14/11
to jna-...@googlegroups.com
Oh, and I'm sorry for the email code snippet.  I really must learn git.  svn is so old-school.

Timothy Wall

unread,
Dec 14, 2011, 6:49:49 PM12/14/11
to jna-...@googlegroups.com
I recall encountering a number of similar bugs when developing a robot-driven test tool (http://abbot.sf.net). Got really inconsistent results across platforms.

You might want to use GetAsyncKeyState rather than GetKeyState; not sure what sorts of things might plug up the input handling queue.

T.

Reply all
Reply to author
Forward
0 new messages