开发者

Can Java see activity of my keyboard?

I would like to write a program which can monitor activity of my keyboard. In more details, the Java program should "see" which key is pressed/released and when. All this information should be stored in a given file.

First of all I would like to know if it's possible to do it with Java. I know that it's possible if I type in a text field generated by Java program. But i开发者_高级运维s it possible for Java to monitor the keyboard activity if I type, let say, in a text field of a browser or, for example, in word (or open office) document?


These events are directed to the window which has the focus, from all events on the desktop you can only get the mouse position.

import java.awt.MouseInfo;


    public class Mouse {
        public static void main(String[] args) {
            while ( true ) {
                System.out.println( MouseInfo.getPointerInfo().getLocation() );
            }
        }
    }

For capturing sysem wide (what you need for word) you need to include a native lib

Code example for windows: Native keyboard mouse hook


Forget JNI and JNA.

There is now a new library for native mouse&keyboard access that works exactly the same as MouseXxxListener and KeyListener. In other words, it's a familiar interface so you don't have to learn anything new (except for how you link an external library).

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


Forget JNI

JNA is the solution (is based on JNI but it's way easier although most people don't use it thinking it's as difficult as JNI, but it's not)

Check https://github.com/twall/jna/ and specifically in the middle of the page where it says: Platform / Platform Documentation

If you download the platform.jar (and check the sourcecode) you'll find a wrapper around User32.dll where you can find the win32 function GetAsyncKeyState(int vKey)

mapped to a corresponding java function

(see h**ps://jna.dev.java.net/javadoc/platform/com/sun/jna/platform/win32/User32.html)

There u can check the state of a key (pressed or released)

If you need a global key listener have a look at my other reply to:

h**p://stackoverflow.com/questions/696170/java-keylistener-without-window-being-open/4394398

Thanks to Stackoverflow i can't post more than one working link in my replies :(

Cheers


It's likely possible to write a Java-based key logger using some native libraries, although be aware that such a program is likely more noticeable than one with a different technology, since the Java VM will need to be running for it to work. Keep that in mind if you're trying to be clandestine.

Also, if you just need such a program for use, and don't have to develop it yourself, there are many hardware and software keylogging systems already out there that you could use instead.


I think that native functions can do it. It's something like you can connect c++ with Java.

http://en.wikipedia.org/wiki/Java_Native_Interface


What you are requesting is not possible using the Java API. In order to do system-wide key logging you need to register with win32 (or other OS native) hooks. Specifically, this will be done using native libraries and interfacing with the JNI.

There are some code snippets over at http://forums.sun.com/thread.jspa?messageID=3808163#3808163. It's a good example of how to get started with JNI and registering a Java callback to a win32 hook.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜