uses System.TimeSpan, System.SysUtils,
Winapi.Windows;
function TTimes.getSinceLastInput(): TTimeSpan;
var
lastInput: TLastInputInfo;
currentTickCount: DWORD;
millisecondsPassed: Double;
begin
lastInput :=
Default(TLastInputInfo);
lastInput.cbSize := SizeOf(TLastInputInfo);
Win32Check( GetLastInputInfo(lastInput) );
currentTickCount := GetTickCount();
// lastInput was before 49.7 days but by now, 49.7 days have passed
if (lastInput.dwTime > currentTickCount)
then
begin
millisecondsPassed :=
(DWORD.MaxValue - lastInput.dwTime)
+
(currentTickCount * 1.0);
// cast to float by multiplying to avoid DWORD overflow
Result := TTimeSpan.FromMilliseconds(millisecondsPassed);
end
else
Result := TTimeSpan.FromMilliseconds(currentTickCount - lastInput.dwTime );
end;