I am writing an Eclipse RCP application that will have other plugin contributions besides my own, and need to determine when the application is idle (i.e. no activity for a period of time, application is minimized, etc.), and when that changes (i.e. application is brought back to the foreground, a mouse is clicked, etc.).
The problem I'm having is that I was going to capture all application keystrokes and mouse moves/clicks...using th开发者_如何学Pythonat to reset a timer, and when the timer is hit, then some idle processing can occur (i.e. informing a server of the idleness - and then again when we switch to active - nothing intensive). However, the application window shell does not receive child events for the various views, etc. so either I'm missing something, or this is the wrong approach.
Can anyone offer a solution? I'm not looking for system-wide idleness - just application idleness.
Thanks.
The Eclipse IDE already has something similar to perform Garbage Collection, take a look at the class org.eclipse.ui.internal.ide.application.IDEIdleHelper in Bundle org.eclipse.ui.ide.application
Maybe that
display.addFilter(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
}
});
is what you're looking for (you can add other events, too)?
This is executed before event handling is done component wise...
But its use is not generally recommended - it should be a very restricted use case, so.
You can use the fact that the readAndDispatch
method will return false when there are no more messages to process. Something like this:
long lastActivityAt = 0;
int idleThresholdSecs = 5;
while (true)
{
while (display.readAndDispatch())
{
lastActivityAt = System.nanoTime();
}
if (System.nanoTime() - lastActivityAt > idleThresholdSecs * 1000 * 1000 * 1000)
{
// we have been idle for at least "idleThresholdSecs"
...
}
else
{
Thread.sleep(50);
}
}
精彩评论