Detect user idle (per application instance)

青春壹個敷衍的年華 提交于 2019-11-30 21:12:55

You could use Application.AddMessageFilter and watch for messages representing user interaction (e.g. mouse, keyboard, maybe menu activation).

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.

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.

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