0

With Java, I need to detect whether a given key (using it's keyCode) is pressed down or not at a certain moment in time.

You might suggest for me to create an event listener for all keys for a certain component and store all keys' states (pressed down or not). However, I need to know whether a key is pressed down in general, and not for a certain Java component.

For example, if my Java application is running in the background and a user has a key pressed down inside OpenOffice, I need to be able to detect so.

How would I accomplish this isKeyDown() functionality that is independent from java components within Java?

Any feedback is appreciated.

Tom
  • 8,536
  • 31
  • 133
  • 232
  • "..if my Java application is running in the background and a user has a key pressed down inside OpenOffice, I need to be able to detect so." You mean like a key-logger? Had you considered using your powers, ..for *good?* – Andrew Thompson Mar 17 '11 at 13:14
  • @Andrew Thompson, no I do not mean for a keylogger, it is merely an example of what I need to do. Please refrain from creating a discussion for a non-existant problem in a technical discussion forum. FYI, I need to complement this to the Robot class to get actual user input rather than what is received by individual components. – Tom Mar 17 '11 at 13:32
  • @Andrew Thompson, also, please stop implying such behaviour about legit people without legit grounds. You have not considered obvious features such as keyboard shortcuts for inactive applications that do deliver benefits "for good". – Tom Mar 17 '11 at 13:40
  • It looks like a similar question to [this one.](http://stackoverflow.com/questions/79658/react-on-global-hotkey-in-a-java-program-on-windows-linux-mac) – z7sg Ѫ Mar 17 '11 at 13:52

1 Answers1

2

Can't be trivially done in Java. At best you can try JNI

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • I was looking for an implementation similar to the Robot class, which sends input rather than observes it. – Tom Mar 17 '11 at 13:34
  • @Tom the Robot class can only work within the scope of Java process, it can't really affect other processes or peek in to the message queues of other processes. Java is an application programming language not a systems programming language – Suraj Chandran Mar 17 '11 at 13:39
  • ah I thought it actually behaved the same way as an external and physical device. Thanks for the help. – Tom Mar 17 '11 at 13:45