Read all the keyboard key pressed values from the system and write into a file, not only from console, entire input from the system like wordpad key press, notpad key press, etc.
-
No i am trying normal program – newuser Jul 30 '13 at 10:03
-
What is wordpad and notepad key press? – Lenymm Jul 30 '13 at 10:06
-
Any where you press keyboard key it will write into a file. Not only console or java application input. – newuser Jul 30 '13 at 10:07
-
You can't. Java has not direct access to hardware, so if you want to listen the keyboard (=make a keylogger) with java you have to use JNI – BackSlash Jul 30 '13 at 10:08
-
@BackSlash I consider your point. – newuser Jul 30 '13 at 10:12
-
yes intresting question? isn't? – Shahrzad Jul 30 '13 at 10:13
-
possible duplicate of [Listening for input without focus in Java](http://stackoverflow.com/questions/901224/listening-for-input-without-focus-in-java) – Lenymm Jul 30 '13 at 10:13
-
@tbodt I am waiting for you – newuser Jul 30 '13 at 10:17
-
I gave up, because of Arnaud's answer, but if you really want my answer, you can find out how to do it yourself. – tbodt Jul 30 '13 at 10:18
3 Answers
No, you can't catch keyevents outside the java program.
If you want to do this, you will have to use OS hooks via a C/C++ library.
If you really want to use it in Java, you can still wrap the C code with JNI...
Here is a blog which explains how to do it on Windows.
As we remember, Java allows to catch keyboard events for a key or for a key combination, but this works only if the Java application frame or console is active at this moment, but if the user opens or select antoher window, the keyboard events will not reach our Java application. Each application has own event(message) queue and the keyboard events will be sent into this queue unless the application window(console) is active.

- 29,980
- 16
- 92
- 148
-
-
Of course. JNI only wraps a C library and provides you some Java interface to access it. So nearly everything possible in C is possible via JNI. – Arnaud Denoyelle Jul 30 '13 at 10:25
Something like this?
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
FileWriter fw = new FileWriter("/file.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(s);
bw.flush();
bw.close();

- 11
- 5
The sole purpose of Java running in JVM is to prevent such thing for security reasons. That's why you need C/C++ to write a key logger as Java can't reach outside JVM.
If you want to log keys from your app:
Read more about KeyListeners (javadoc)
And about file writing (example)
Probably duplicate of Listening for input without focus in Java
-
-
Boss i know how to write into file. And also know about KeyListener. I need the basic idea is this possible or not. – newuser Jul 30 '13 at 10:09