4

I want this to happen:

  1. Start my java program
  2. My java program launches notepad.exe
  3. If I type a letter in notepad my java program should react and print out the letter

In short: Is it possible to make a key event listener for another process? The java program won't have focus.

If it's impossible I want to be able to change focus between my Java swing window and the notepad process. For example:

A) My java program has focus (small swing window), I type the letter "A"
B) Notepad is given focus quickly and the letter A is typed there (for example using the Robot class)
C) My java program gets focus again


Quoting the answer given in Creating a keyboard hook for a Java program that sits in the System Tray

so , creating a keyboard hook isn't as easy as it looks , java doesn't provide a direct way to interact with the system events for purposes of security ; as it might lead to serious problems concerning the JVM , system's memory and the portability of Java Framework..

you have 4 ways to make global keyboard hooks :

  1. Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
  2. Use JIntellitype , which - as you said - issues some problems.
  3. the elegant solution by Sergei Biletnikov here http://biletnikov-dev.blogspot.com/2009/09/global-hotkeys-for-java-applications_25.html
  4. ignore Java , and try Autoit V3 ( I'm not 100% sure about it , but I think you could send signals/events from Autoit to your Java app , so Autoit would just work as a bridge that catches the global key strokes)

Going with number 3, which is a good tutorial. If the link has 404'd maybe the project source is still up: gigapeta.com/dl/1917618aba749a

Community
  • 1
  • 1
user1405714
  • 41
  • 1
  • 3
  • Also: Only has to work on Windows. If I need to do some native stuff, please tell me how – user1405714 May 20 '12 at 00:19
  • But, you can still be anonymous you know...I am anon right now, for example. You have absolutely 0 clue about who I am. – OmniOwl May 20 '12 at 00:22
  • 1
    What you are trying to do goes against Java's security model. It's not possible. – Jivings May 20 '12 at 00:23
  • Vipar: You are vipar and I can track exactly what you have been doing on stackoverflow. You don't seem to grasp the concept of being anonymous. I don't know who you are IRL, but you do have an identity. – user1405714 May 20 '12 at 00:27
  • Jivings: There are ways around it, even if I have to use some JNI code (that I've never used before). Someone out there in the world knows how, I just need to be lucky and reach him. – user1405714 May 20 '12 at 00:29
  • Take a look [here](http://stackoverflow.com/a/10598169/758280) – Jeffrey May 20 '12 at 00:33
  • Perhaps the bigger question is *why* do you want to do it? Overall, what is it you're trying to achieve? – Bohemian May 20 '12 at 00:42
  • Jeffrey: Thank you, exactly what I need! Bohemian: No, the biggest question is not why. That is irrelevant. You probably think I want to make a password listener or something. But I'll tell you, I want to make it possible to control your character in Diablo 3 using the keyboard instead of the mouse. Just something I thought of and figured it would be fun to do. I like programming more than I like playing games after all. – user1405714 May 20 '12 at 00:48
  • Then you should put that in your question. Maybe there's another why to do this. You've asked about a specific implementation, when another one might be better. A little info like that can help others give better answers. – Bohemian May 20 '12 at 01:00
  • I would be really, really surprised if there was _any_ way to do this. – Louis Wasserman May 20 '12 at 01:11

1 Answers1

2

With your presented solution #1:

Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.

You should take a look at this stackoverflow thread:

JNA Keyboard Hook in Windows

There is copy/pastable code that demonstrates a JNA key hook. You will need to download the JNA dependencies from http://jna.java.net/ for the example to work. You won't even need to fiddle with silly DLL's.

Additionally, you might want to give this library a try. While I have no experience with it, it popped up on my google search for "java keyboard hook." It seems to accomplish the goal of intercepting the keystrokes:

http://code.google.com/p/jnativehook/

Note that you would need some additional native code to see what the current "active window" is. If the active window matches "Notepad.exe" then you should record a native key event.

Good luck!

Community
  • 1
  • 1
Alex Lynch
  • 951
  • 5
  • 11