Detecting If Computer Is Idle Based on Mouse and Keyboard Interactions

Detecting if computer is idle based on mouse and keyboard interactions

The XScreensaver extension in the X server tracks keyboard and mouse idle time. If you download the source code to Psi, their source tree has a file idle_x11.cpp that contains logic to query the idle time. Use that as an example.

The basic idea is to use XScreenSaverQueryInfo to read the screen saver status info.

How to detect inactive user

To track a user's idle time you could hook keyboard and mouse activity. Note, however, that installing a system-wide message hook is a very invasive thing to do and should be avoided if possible, since it will require your hook DLL to be loaded into all processes.

Another solution is to use the GetLastInputInfo API function (if your application is running on Win2000 (and up) machines).
GetLastInputInfo retrieves the time (in milliseconds) of the last input event (when the last detected user activity has been received, be it from keyboard or mouse).

Here's a simple example. The SecondsIdle function returns a number of second with no user activity (called in an OnTimer event of a TTimer component).

~~~~~~~~~~~~~~~~~~~~~~~~~
function SecondsIdle: DWord;
var
liInfo: TLastInputInfo;
begin
liInfo.cbSize := SizeOf(TLastInputInfo) ;
GetLastInputInfo(liInfo) ;
Result := (GetTickCount - liInfo.dwTime) DIV 1000;
end;

procedure TForm1.Timer1Timer(Sender: TObject) ;
begin
Caption := Format('System IDLE last %d seconds', [SecondsIdle]) ;
end;

http://delphi.about.com/od/adptips2004/a/bltip1104_4.htm

C# - Detect time of last user interaction with the OS

GetLastInputInfo. Documented at PInvoke.net.

Do something after some inactivity on the computer

Simplest google search brought up this:

http://dataerror.blogspot.co.il/2005/02/detect-windows-idle-time.html
Should work

Flex: detecting user idle?

See also the idle event in SystemManager. This approach works for AIR or Flash Player.

application.systemManager.addEventListener(FlexEvent.IDLE, onIdle);

You can get the idle time (in an unsupported way) using

SystemManager.mx_internal::idleCounter

Detect user idle (per application instance)

You could use Application.AddMessageFilter and watch for messages representing user interaction (e.g. mouse, keyboard, maybe menu activation).



Related Topics



Leave a reply



Submit