2

I was wondering if there is anyway to catch a KeyEvent outside of my Java program. I know that using the normal Java, you can only catch KeyEvents that are inside your Java program. From what I have read about this on different forums, I think that catching KeyEvents outside your program is all OS based but i'm using a Windows 7 if that helps anyone. If anyone knows how to achieve this, please answer back and if you don't mind a link too a tutorial or an example would be awesome. Thanks in advance.

XliteRSPS
  • 106
  • 10
  • What do you mean by *"Catching KeyEvents Outside of Program"*? What is your objective? – Sujay Sep 13 '12 at 23:15
  • When I press "a" and my program isn't in focus it'll pick up the KeyEvent and will print out the character "a" – XliteRSPS Sep 13 '12 at 23:17
  • 2
    No, don't use Java as a key logger as it is built to be OS agnostic and thus somewhat removed from the OS. Use C, C++ or some other language that gets closer to the OS for this nefarious purpose. – Hovercraft Full Of Eels Sep 14 '12 at 00:49

1 Answers1

1

This question might help:

Java Keyboard/Mouse activity (even outside of my app)

It's not an exact duplicate, because it just wants to know whether or not there have been key events, but it gives you a pointer as to what to do:

Use JNI.


In addition:

The code in the link in the question http://www.jotschi.de/?p=90 doesn't work in Windows 7/Vista, but there is a newer version:

http://ksquared.de/blog/2011/07/java-global-system-hook/

And this, I think (I haven't looked at it in depth) is a nice tutorial.

It has ALL of the code you need for this.

Community
  • 1
  • 1
eboix
  • 5,113
  • 1
  • 27
  • 38
  • I understand the main concept of that but I think it's a little overkill. Didn't help me much at all. – XliteRSPS Sep 13 '12 at 23:22
  • Wait, but it is the complete source. It's everything you need, written out for you! – eboix Sep 13 '12 at 23:25
  • Sorry, I didn't see you added on to your answer. I'm gonna go study this for a while and see what I can do. Thanks – XliteRSPS Sep 13 '12 at 23:27