5

I need to detect when the mouse or keyboard activity is present. My application runs in the background (tray) and I need to detect this activity even when my app isn't in focus.

I don't need to know what keys were pressed, but simply WHEN they are pressed.

Can I do this with just Java? I have read some solutions using JNI and C++/C but they seem to be OS specific. Is there an easy solution to this that will work with Windows, Linux, and Mac?

Thanks!

Andy Hin
  • 30,345
  • 42
  • 99
  • 142
  • 1
    This sort of thing is necessarily platform-specific, since different OS platforms offer this feature in different ways, and some configurations might not even offer it at all. – Greg Hewgill Aug 16 '10 at 03:55
  • OK UPDATE: So, I've found a library for windows (jintellitype) and for linux (jxgrabkey). Does anyone know of one for MAC OSX? – Andy Hin Aug 16 '10 at 04:18
  • I'm going to probably say that this is not going to be a question that you'll get a good answer for. In essence you want your program to behave like a key logger, and those usually have to hook themselves into the kernel. Java doesn't have the power to do that so you're out of luck man. – BT. Aug 16 '10 at 04:19

1 Answers1

3

Since JNI is required, which relys on the OS specific APIs, the only way would be to have an implementation for each supported OS and load it dynamically after detecting the OS in you application.

For Windows Java Global Keyboard/Mouse Hook – JNI

stacker
  • 68,052
  • 28
  • 140
  • 210
  • With permision of Jotschi (the original author of the Keyboard/Mouse Hook), I took his library years ago and created a new version. Please find the latest release and examples [on GitHub](https://github.com/kristian/system-hook). As well as a description of the library on [the blog post](http://kra.lc/blog/2016/02/java-global-system-hook/). – Kristian Kraljic Feb 10 '16 at 22:43