判断软件的闲置时间

//GetLastInputInfo是检测系统输入的,应用到某个程序中不合适!

此问题有二种解法来监控输入消息:

1.用线程级HOOK,钩上MOUSEHOOK与KEYBOARDHOOK

2.在Application.OnMessage中做处理

显然,用第2种方法比较方便!

众所周知,键盘与鼠标消息都是队列消息,需要经过消息队列后经过一些处理,再发往各个WNDPROC。基于这个原理,我们可以在ONMESSAGE中做一下消息处理,即先声明全局变量,用于保存上次输入动作的时间,而后用时间控件来进行定时扫描,用GetTickCount来判断是否超时。

 

 

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, AppEvnts, ExtCtrls, StdCtrls;

type

  TForm1 = class(TForm)

    ApplicationEvents1: TApplicationEvents;

    tmr1: TTimer;

    edt1: TEdit;

    procedure ApplicationEvents1Message(var Msg: tagMSG;

      var Handled: Boolean);

    procedure tmr1Timer(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

  ElapsedTime:Longint;

implementation

{$R *.dfm}

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;

  var Handled: Boolean);

begin

  case Msg.message of

    WM_KEYDOWN,WM_LBUTTONDOWN,WM_LBUTTONUP://各种键盘与鼠标消息

    begin

      ElapsedTime:=GetTickCount;

      //Handled:=True;这里不要把Handled设置为True,否则消息就不分发至WNDPROC了

    end;

  end;

end;

procedure TForm1.tmr1Timer(Sender: TObject);

begin

   if GetTickCount- ElapsedTime>60000 then

   begin

     tmr1.Enabled:=false;

     ShowMessage('1分钟软件没有操作了');

   end;

end;

end.
View Code

 

你可能感兴趣的:(时间)