views:

92

answers:

3

This is a rather tricky question as I have found no information online. Basically, I wish to know how to check if a computer is idle in Java. I wish a program to only work if the computer is in active use but if it is idle then to not.

The only way i can think of doing this is hooking into the mouse/keyboard and having a timer.

MSN Messenger has that "away" feature, I wish for something similar to this.

A: 

Nothing in the platform-independent JRE will answer this question. You might be able to guess by measuring clock time for a calculation, but it wouldn't be reliable. On specific platforms, there might be vendor APIs that might help you.

bmargulies
Its not so much system idle time i need to find. I just need to find it a user has been using there PC within say the last 10 mins. Basically. I want notifications to only show if the user is actually at their PC.
Scott Straughan
I think that this is even more impossible than CPU time usage.
bmargulies
+1  A: 

Java has no way of interacting with the Keyboard, or Mouse at the system level outside of your application.

That being said here are several ways to do it in Windows. The easiest is probably to set up JNI and poll

GetLastInputInfo

for keyboard and mouse activity.

Romain Hippeau
A: 

1) Make a new thread.

2) Give it a super super low priority (the lowest you can)

3) Every second or two, have the thread do some simple task. If super fast, at least 1 CPU is prolly idle. If it does it slow, then at least 1 core is prolly not idle.

Or

Just run your program at a low priority. That will let the OS deal with letting other programs run over your program.

bwawok