Can I detect if key is pressed in another window using java

219 Views Asked by At

I want to create a program that would run in the background and everytime the user presses a key, the program would notify him in which language he is writing. but I don't know how to detect (if possible) a key pressing in another unrealated program… As far as I know keylisteners are only for my own java program when it has the focus, so it is not that useful for me… (Sorry for my poor english)

1

There are 1 best solutions below

0
On

Try JNativeHook, it does this nicely.

Only caveat is that the key codes returned are different from those that Java uses IIRC.

I could show you a working program but Stack Overflow doesn't let me link to it because that would be "advertising". No time to produce an SSCCE for this space right now, sorry. I think JNativeHook is not hard to get into anyway.