function GetTimeSinceLastUserInputInWindows(): TTimeSpan;
var
lastInput: TLastInputInfo;
currentTickCount: DWORD;
millisecondsPassed: Double;
begin
lastInput := Default(TLastInputInfo);
lastInput.cbSize := SizeOf(TLastInputInfo);
Win32Check( GetLastInputInfo(lastInput) );
currentTickCount := GetTickCount();
if (lastInput.dwTime > currentTickCount) then begin // lastInput was before 49.7 days but by now, 49.7 days have passed
millisecondsPassed :=
(DWORD.MaxValue - lastInput.dwTime)
+ (currentTickCount * 1.0); // cast to float by multiplying to avoid DWORD overflow
Result := TTimeSpan.FromMilliseconds(millisecondsPassed);
end else begin
Result := TTimeSpan.FromMilliseconds(currentTickCount - lastInput.dwTime );
end;
end;
1条答案
按热度按时间vjrehmav1#
此解决方案适用于
a)自上次在Windows中输入以来的时间
每次鼠标移动或键盘输入都会将时间重置为零。
https://www.delphipraxis.net/1504414-post3.html