Detect user idle (per application instance)

巧了我就是萌 提交于 2019-12-09 00:52:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!