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
Using Hardware Performance Counters in Linux
Mpirun: Unrecognized Argument Mca
The Simplest and Neatest C++11 Scopeguard
Change the Current Working Directory in C++
How to Compare Multiple Strings Inside an If Statement
A Warning - Comparison Between Signed and Unsigned Integer Expressions
Where Is the Lock for a Std::Atomic
C++ Warning: Deprecated Conversion from String Constant to 'Char*' [-Wwrite-Strings]
What Range of Values Can Integer Types Store in C++
How to Evaluate Mathematical Expressions in C++
Get Computer Name and Logged User Name
Way Cross Compile C/C++ Code to Run on Windows, Linux, and MAC Os
Declaring the Array Size with a Non-Constant Variable
C++11: I Can Go from Multiple Args to Tuple, But How to Go from Tuple to Multiple Args
How to Update an Existing Element of Std::Set
Why Are My Struct's Members Not Properly Initialised Using '{}'