如何检测不活跃用户

发布于 2024-08-20 11:03:46 字数 69 浏览 1 评论 0原文

如何检测 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷梦 2024-08-27 11:03:46

要跟踪用户的空闲时间,您可以挂钩键盘和鼠标活动。但请注意,安装系统范围的消息挂钩是一件非常具有侵入性的事情,应该尽可能避免,因为它需要将挂钩 DLL 加载到所有进程中。

另一个解决方案是使用 < code>GetLastInputInfo API 函数(如果您的应用程序运行在 Win2000(及更高版本)计算机上)。
GetLastInputInfo 检索最后一个输入事件的时间(以毫秒为单位)(当收到最后检测到的用户活动时,无论是来自键盘还是鼠标)。

这是一个简单的例子。 SecondsIdle 函数返回没有用户活动的秒数(在 TTimer 组件的 OnTimer 事件中调用)。

~~~~~~~~~~~~~~~~~~~~~~~~~
function SecondsIdle: DWord;
var
   liInfo: TLastInputInfo;
begin
   liInfo.cbSize := SizeOf(TLastInputInfo) ;
   GetLastInputInfo(liInfo) ;
   Result := (GetTickCount - liInfo.dwTime) DIV 1000;
end;

procedure TForm1.Timer1Timer(Sender: TObject) ;
begin
   Caption := Format('System IDLE last %d seconds', [SecondsIdle]) ;
end;

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 an OnTimer event of a TTimer component).

~~~~~~~~~~~~~~~~~~~~~~~~~
function SecondsIdle: DWord;
var
   liInfo: TLastInputInfo;
begin
   liInfo.cbSize := SizeOf(TLastInputInfo) ;
   GetLastInputInfo(liInfo) ;
   Result := (GetTickCount - liInfo.dwTime) DIV 1000;
end;

procedure TForm1.Timer1Timer(Sender: TObject) ;
begin
   Caption := Format('System IDLE last %d seconds', [SecondsIdle]) ;
end;

http://delphi.about.com/od/adptips2004/a/bltip1104_4.htm

蓝天白云 2024-08-27 11:03:46

当屏幕保护程序即将启动时,您的应用程序将收到一条以 SC_SCREENSAVE 作为命令 ID 的 WM_SYSCOMMAND 消息。这样可以吗?当监视器即将变为空白时,还有 SC_MONITORPOWER 命令 ID(也是 WM_SYSCOMMAND 消息)。

编辑:查看评论,您似乎并不关心用户是否处于非活动状态,而是关心您的应用程序是否处于非活动状态。

这很容易。如果您的应用程序最小化,则用户不会与其交互。如果您的应用程序不是前台应用程序,那么这也是一个很好的指示器。

您还可以关注泵中的消息,以注意是否有任何用户输入消息到您的应用程序,在 C++ 中,向泵添加代码很简单,在 delphi 中,您可以使用 WH_GETMESSAGE 挂钩来监视泵< /strike> 挂钩到 TApplication 实现的消息循环。或获取最后输入信息

Your application will get a WM_SYSCOMMAND message with SC_SCREENSAVE as a command id when the Screen Saver is about to kick in. Would that do? there's also the SC_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 pump hook into the message loop that TApplication implements. Or GetLastInputInfo

昇り龍 2024-08-27 11:03:46

这个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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文