如何检测不活跃用户
如何检测 Windows 应用程序中的非活动(空闲)用户?当用户在一段时间内没有任何输入(键盘、鼠标)时,我想关闭应用程序。
How to detect inactive (idle) user in Windows application? I'd like to shutdown application when there hasn't been any input (keyboard, mouse) from user for certain period of time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要跟踪用户的空闲时间,您可以挂钩键盘和鼠标活动。但请注意,安装系统范围的消息挂钩是一件非常具有侵入性的事情,应该尽可能避免,因为它需要将挂钩 DLL 加载到所有进程中。
另一个解决方案是使用 < code>GetLastInputInfo API 函数(如果您的应用程序运行在 Win2000(及更高版本)计算机上)。
GetLastInputInfo
检索最后一个输入事件的时间(以毫秒为单位)(当收到最后检测到的用户活动时,无论是来自键盘还是鼠标)。这是一个简单的例子。
SecondsIdle
函数返回没有用户活动的秒数(在TTimer
组件的OnTimer
事件中调用)。http://delphi.about.com/od/adptips2004/a/bltip1104_4.htm
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 anOnTimer
event of aTTimer
component).http://delphi.about.com/od/adptips2004/a/bltip1104_4.htm
您可能想查看此问题的答案:如何判断 Windows 何时处于非活动状态[1] 这基本上是相同的问题,建议的解决方案是使用
GetLastInputInfo
[2] API 调用。这篇文章还解释了一些方面: (The Code Project) How to check for user inactivity with并且在 C# 中没有平台调用 [3]
[1] 如何告知 Windows 何时处于非活动状态
[2] http://msdn.microsoft.com /en-us/library/ms646302%28VS.85%29.aspx
[3] http://www.codeproject.com/KB/cs/uim.aspx
You might want to see the answer to this question: How to tell when Windows is inactive [1] it is basically same question the solution suggested is to use the
GetLastInputInfo
[2] API call.This post explains some aspects as well: (The Code Project) How to check for user inactivity with and without platform invokes in C# [3]
[1] How to tell when Windows is inactive
[2] http://msdn.microsoft.com/en-us/library/ms646302%28VS.85%29.aspx
[3] http://www.codeproject.com/KB/cs/uim.aspx
当屏幕保护程序即将启动时,您的应用程序将收到一条以
SC_SCREENSAVE
作为命令 ID 的WM_SYSCOMMAND
消息。这样可以吗?当监视器即将变为空白时,还有 SC_MONITORPOWER 命令 ID(也是 WM_SYSCOMMAND 消息)。编辑:查看评论,您似乎并不关心用户是否处于非活动状态,而是关心您的应用程序是否处于非活动状态。
这很容易。如果您的应用程序最小化,则用户不会与其交互。如果您的应用程序不是前台应用程序,那么这也是一个很好的指示器。
您还可以关注泵中的消息,以注意是否有任何用户输入消息到您的应用程序,在 C++ 中,向泵添加代码很简单,在 delphi 中,您可以
使用 WH_GETMESSAGE 挂钩来监视泵< /strike> 挂钩到 TApplication 实现的消息循环。或获取最后输入信息Your application will get a
WM_SYSCOMMAND
message withSC_SCREENSAVE
as a command id when the Screen Saver is about to kick in. Would that do? there's also theSC_MONITORPOWER
command id when the monitor is about to blank (also a WM_SYSCOMMAND message).Edit: looking at the comments, it appears that you don't care about whether the user is inative, but rather whether your application is inactive.
This is easy. If your app is minimized, then the user isn't interacting with it. If your app is not the foreground application, that's a good inicator as well.
You could also pay attention to messages in your pump to notice if there have been any user input messages to your app, In C++ adding code to the pump is trivial, in delphi you can
use a WH_GETMESSAGE hook to monitor the pumphook into the message loop that TApplication implements. Or GetLastInputInfo这个SecondsIdle根本不起作用。
方法是使用 TTimer 与第二个变量相结合,每次用户输入鼠标或键盘时该变量都会重置。
This SecondsIdle doens't work at all.
The way is to use a TTimer combined with a second variable that resets every time user inputs mouse or keyboard.