检测用户超过多长时间没有操作键盘或鼠标
procedure TForm1.Timer1Timer(Sender: TObject);
var
vLastInputInfo: TLastInputInfo;
begin
vLastInputInfo.cbSize := SizeOf(vLastInputInfo);
// GetLastInputInfo 是系统级别的检测
GetLastInputInfo(vLastInputInfo);
if GetTickCount - vLastInputInfo.dwTime > 5000 then
begin
timer1.Enabled:= false;
showmessage('超过5秒,用户未动鼠标!');
end;
end;
如果要进行应用程序级别的检测,可使用 TApplicationEvents 控件,

在窗口中放置一个TApplicationEvents控件,在其 OnMessage 事件中写入以下代码:
var
ElapsedTime:Longint;
procedure TForm1.Timer1Timer(Sender: TObject);
var
vLastInputInfo: TLastInputInfo;
begin
if GetTickCount - ElapsedTime > 10000 then
begin
Timer1.Enabled:=false;
ShowMessage('1分钟软件没有操作了');
end;
end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
case Msg.message of
WM_KEYDOWN, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE://各种键盘与鼠标消息
begin
ElapsedTime := GetTickCount;
//Handled:=True;这里不要把Handled设置为True,否则消息就不分发至WNDPROC了
end;
end;
end;
利用该控件也可以进行程序级别的热键检测:
procedure TForm1.ApplicationEventsMessage(var Msg: tagMSG; var Handled: Boolean);
var
AktywnaForma: TForm;
begin
// 检查是否按下 CTRL 键并同时按下 F7 键
if (Msg.wParam = VK_F7) and ((GetKeyState(VK_CONTROL) and $8000) <> 0) and Assigned(Screen.ActiveForm) then
begin
ShowMessage('CTRL+F7');
// 在这里执行你想要的操作
end;
end;

浙公网安备 33010602011771号