Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect user idle (per application instance)

I need user idle detection in my application only (per "my application instance").
I can't use GetLastInputInfo because is session-specific user input:

GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function.

Any suggestions ?

like image 901
halorty Avatar asked Feb 15 '11 14:02

halorty


1 Answers

I assume you want to detect the idle time but only when you application is active. You then have to define exactly what you mean by your application being active. When your application is active (by your own definition) you can at regular intervals call GetLastInputInfo to determine the idle time of your application when it is active (e.g. using a timer of some sort).

Windows has a concept of a foreground window and the current foreground window can be retrived using GetForegroundWindow. You can use GetWindowThreadProcessId to find the process ID of the process owning the foreground window. If that process ID is your process ID you know that you are the foreground process even if your application has multiple windows. You will have to do this detection at regular intervals just as you have to check the idle time.

You should not be concerned about GetLastInputInfo only providing information about the session. If multiple users are logged onto the same Windows computer they will each have their own session, but another user being idle or not ilde in a session should not affect how you detect if the user of your application is idle.

like image 67
Martin Liversage Avatar answered Sep 21 '22 01:09

Martin Liversage