-1

I want to perform background tasks (Updates, Backups, Calculations, ...) at a time when nobody is using my application.

Therefore, I want to determine the time since the last keystroke and/or mouse move in my application. If there is no user activity for more than a specific time, the chance is high not to disturb a user. Multithreading is not an option for me.

I want to avoid touching every single OnMouseDown-/OnKeyPress-Event of every component in my application because this doesn't make any sense.

How can I get

a) The time since last input in Windows

b) The time since last input in my application

Ken White
  • 123,280
  • 14
  • 225
  • 444
Michael Hutter
  • 1,064
  • 13
  • 33
  • 2
    For b, use a `TApplicationEvents` and its `OnMessage` event. Using this you can easily detect any mouse or keyboard input in your application (including mouse moved) with only ~10-20 lines of code in a single place. – Andreas Rejbrand Oct 20 '22 at 06:56

1 Answers1

1

This solution works for problem
a) The time since last input in Windows
Every mouse move or keyboard input resets the time to zero.

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;

https://www.delphipraxis.net/1504414-post3.html

Michael Hutter
  • 1,064
  • 13
  • 33