问题
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 ?
回答1:
You could use Application.AddMessageFilter and watch for messages representing user interaction (e.g. mouse, keyboard, maybe menu activation).
回答2:
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.
回答3:
If this is a Windows Forms application, you can create an idle event handler and subscribe to the Application.Idle event. You'll get a notification when the application finishes processing and is about to enter the idle state.
来源:https://stackoverflow.com/questions/5004614/detect-user-idle-per-application-instance