Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect inactive user

How to detect inactive (idle) user in Windows application? I'd like to shutdown application when there hasn't been any input (keyboard, mouse) from user for certain period of time.

like image 456
Harriv Avatar asked Feb 06 '10 10:02

Harriv


People also ask

How do I find inactive users?

To find the accounts, run a script that queries Active Directory for inactive user accounts. In Active Directory Module for Windows PowerShell, Search-ADAccount –AccountInactive –UsersOnly command returns all inactive user accounts.

What is considered inactive user?

Inactive User License Definition:An Inactive User is any associate user with a status of inactive in the software application without access to the application, curricula or other functionality.

What is an idle detection?

The Idle Detection API notifies developers when a user is idle, indicating such things as lack of interaction with the keyboard, mouse, screen, activation of a screensaver, locking of the screen, or moving to a different screen. A developer-defined threshold triggers the notification.


Video Answer


2 Answers

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

like image 185
James Campbell Avatar answered Sep 20 '22 15:09

James Campbell


You might want to see the answer to this question: How to tell when Windows is inactive [1] it is basically same question the solution suggested is to use the GetLastInputInfo [2] API call.

This post explains some aspects as well: (The Code Project) How to check for user inactivity with and without platform invokes in C# [3]

[1] How to tell when Windows is inactive
[2] http://msdn.microsoft.com/en-us/library/ms646302%28VS.85%29.aspx
[3] http://www.codeproject.com/KB/cs/uim.aspx

like image 23
Tommy Andersen Avatar answered Sep 16 '22 15:09

Tommy Andersen