不用处理鼠标键盘消息,只要timer一直enable,只要鼠标键盘有操作它会自动重新计时。
procedure TForm1.Timer1Timer(Sender: TObject);
var
vLastInputInfo: TLastInputInfo;
begin
vLastInputInfo.cbSize := SizeOf(vLastInputInfo);
GetLastInputInfo(vLastInputInfo);
if GetTickCount - vLastInputInfo.dwTime > 3000 then
begin
//我刚才showmessage演示一下,所以才使用timer.enable:= false,把他们都去掉直接
//在这里放注销用户,重新登录的代码,就可以了。不要关闭timer
end;
end;
type
TForm1 = class(TForm)
Timer1: TTimer;
ApplicationEvents1: TApplicationEvents;
procedure Timer1Timer(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
var
vLastInputInfo: TLastInputInfo;
begin
vLastInputInfo.cbSize := SizeOf(vLastInputInfo);
GetLastInputInfo(vLastInputInfo);
if GetTickCount - vLastInputInfo.dwTime > 3000 then
begin
timer1.Enabled:= false;
showmessage('超过3秒,已锁定!');
end;
end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if (msg.message = WM_KEYDOWN) or (msg.message = WM_MOUSEMOVE) or (Msg.message = WM_MOUSEACTIVATE) then
if not Timer1.Enabled then timer1.Enabled := true;
end;