is it even possible to build (in JAVA) application that gets keyboard strokes done on any application in windows (not in java application it self) ?!!
5 Answers
Yes I believe it is possible, but you'd have to use JNI to do it as it requires use of the Windows API as described here.
-
1If accessing native code, I'd suggest using [JNA](https://jna.dev.java.net/) instead. – darioo Feb 08 '11 at 13:10
-
@darioo: Yeah that looks like the thing to use. – trojanfoe Feb 08 '11 at 13:13
Yes, throught JNI using the Windows API. But it will not be portable.

- 18,136
- 30
- 106
- 167
First, this is right that you can capture events on the java application window only. Second, it is wrong that your problem may be solved using JNI API only. I tried the following trick.
Create transparent full screen window. Capture events on it. When you captured the keystroke quickly disappear the window and simulate the same key press using Robot. Then create the transparent full screen window again.
This is ugly trick but it is pure java and user does not see anything. Good luck.

- 114,158
- 16
- 130
- 208
Wow.. look at the way people are answering..
3 mins ago some one suggested JNI.
Yes, throught JNI using the Windows API. But it will not be portable.
1 min ago some one else thought of answering too..
Yes I believe it is possible, but you'd have to use JNI to do it as it requires use of the Windows API as described here.
24 seconds ago someone else thought about it too...
Java by itself does not have any facility to perform this kind of thing. You have to use JNI to create a native DLL using the Windows API that will be called by your Java code through the System.loadLibrary(String libname) method.
I too think the same thing..
Use JNI dude.. :D
We need something in SF to prevent this !! If someone already suggested JNI, others shouldn't be able to post the same.. Let them add to the comment or upvote the already existing comment..!!
edit - and yeah, I am waiting for downvotes and critics here.. Let it begin!!

- 7,888
- 13
- 48
- 85
-
-
oh, and I see there is a question in meta already explaining this : http://meta.stackexchange.com/questions/49554/prevent-duplicate-answer-posts-to-the-same-question – Shrinath Feb 08 '11 at 13:17
-
My pleasure. I can't turn down a request! I think possibly since you wrote this SO has updated it so that when you're writing an answer it says "another answer has just been posted, do you want to see it?" to avoid this embarrassment. We can hope anyway :) – rogerdpack Sep 18 '13 at 23:47