Link to home
Start Free TrialLog in
Avatar of ChrisJonesLycos
ChrisJonesLycos

asked on

Detecting Idle Time in Delphi

I have an app that needs to detect when the whole system has not received any input from keyboard or mouse for a few minutes. I've found some very complicated hooking examples online which involve DLLs and some others that simply don't work. Can anyone suggest a straight forward way of doing this.
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

what about using GetLastInputInfo?
edit: you need it systemwide, so this function can't help you :(

function SecondsIdle: DWord;
var
  liInfo: TLastInputInfo;
begin
  liInfo.cbSize := SizeOf(TLastInputInfo);
  GetLastInputInfo(liInfo);
  Result := (GetTickCount - liInfo.dwTime) DIV 1000;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Caption := Format('System IDLE last %d seconds', [SecondsIdle]);
end;

Open in new window

Avatar of ChrisJonesLycos
ChrisJonesLycos

ASKER

The MSDN documentation says that that's for your own window only, not system wide.
No wait, it says literaly "GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function"
It speaks about sessions, not "window"
Is that not the same? I'll try out some code and see what result I get.
For a session I mean a User session. If you have some user remotely connected that's a separate session I guess. It's like it works into HKCU instead of HKLM
Anyway, everywhere I read, the suggested approach for a system idle detection is this.
I've requested that this question be closed as follows:

Accepted answer: 0 points for ChrisJonesLycos's comment #37782640

for the following reason:

Cool. You're right, code works! Session obviously doesn't mean just your own app, it's definitely system wide. Many thanks.
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I accepted your solution. Not sure what happened.