如何检测不活跃的用户 [英] How to detect inactive user

查看:85
本文介绍了如何检测不活跃的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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.

推荐答案

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

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).

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

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

这篇关于如何检测不活跃的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆