Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Detecting user activity in other programs

Im doing now a program which measures user activity. That program is in background, but it should be able to tell if user is doing something on computer or not. Now Im able to detect only mouse movements (by getting each second mouse X and Y and comparing them to precedent values) but how can I see if user is using only keyboard in other programs? Is there a way to do it in Java?
[382 byte] By [sergio] at [2007-11-11 6:48:44]
# 1 Re: Detecting user activity in other programs
Just as you have mouse events, you have keyboard events which your program can monitor. So the trick is creating an "EventListener" which can monitor and capture keyboard input even when your program does not have focus ... If you are doing this with mouse events, won't the same mechanism work for key events?
nspils at 2007-11-11 22:40:14 >
# 2 Re: Detecting user activity in other programs
Just as you have mouse events, you have keyboard events which your program can monitor. So the trick is creating an "EventListener" which can monitor and capture keyboard input even when your program does not have focus ... If you are doing this with mouse events, won't the same mechanism work for key events?
Do you think I would have asked this question if it were so simple? :)
An window doesn't receive event's if it doesn't have focus, the only way to check mouse is like this:

PointerInfo pi = MouseInfo.getPointerInfo();
Point po = pi.getLocation();

and compare new pointer with old one.
sergio at 2007-11-11 22:41:20 >
# 3 Re: Detecting user activity in other programs
Here is an article which seems to hold the kernel of how to approach your issue - using the JNI to interact with the operating system

http://www.javaworld.com/javaworld/jw-09-2001/jw-0928-ntmessages.html

and

http://www.dev-archive.com/Java/Article/21841/1763/page/1

Sun's JNI tutorial and other info can be found here:

http://java.sun.com/docs/books/tutorial/native1.1/

http://java.sun.com/j2se/1.4.2/docs/guide/jni/

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
nspils at 2007-11-11 22:42:18 >
# 4 Re: Detecting user activity in other programs
Here is an article which seems to hold the kernel of how to approach your issue - using the JNI to interact with the operating system

Nspils, thank you very much for answering my posts, but I was hoping that there is a cross platform Java solution to this problem.
sergio at 2007-11-11 22:43:22 >
# 5 Re: Detecting user activity in other programs
Nspils, thank you very much for answering my posts, but I was hoping that there is a cross platform Java solution to this problem.

I don't exactly know what you mean by "cross platform Java solution". I think you mean that you hope it would be possible with only Java. But I think nspils is right; for accessing the mouse or keyboard you'll need a more low level language, JNI would be an option for you to create native methods in Java.
prometheuzz at 2007-11-11 22:44:15 >
# 6 Re: Detecting user activity in other programs
I don't exactly know what you mean by "cross platform Java solution". I think you mean that you hope it would be possible with only Java.
Yes, that is correct.

But I think nspils is right; for accessing the mouse or keyboard you'll need a more low level language, JNI would be an option for you to create native methods in Java.
But I can access mouse with only Java. And if Sun created MouseInfo, why they have not created KeyboardInfo?
sergio at 2007-11-11 22:45:20 >