[fpc-pascal] Getting Last User Input reliably

James Richters james.richters at productionautomation.net
Fri Dec 3 14:52:06 CET 2021


I'm trying to get the time lapsed since the last user input with keyboard or
mouse on a Windows PC.  For this I am doing:

GetLastInputInfo(Last_Input_Info);
IdleTime:= (GetTickCount - Last_Input_Info.dwTime) DIV 1000;

This works most of the time, but every 25 days or so GetTickCount rolls
over, and so if there was activity before the roll over and then no activity
until after the roll over, then this whole scheme just produces incorrect
results.  

Is there a better way of doing this that doesn't have this issue?  Most of
the PCs I run this on have not been rebooted or shutdown for years so this
happens quite often.
Is there some function in FPC that would give me the time elapsed since last
user input?

If there isn't a better way to detect the amount of time since last user
input, then is there a way I can reliably detect the roll over and correct
for it?

I thought I might be able to fix this by using GetTickCount64 and comparing
it to GetTickCount to see if GetTickCount rolled over, but that doesn't
actually help a lot.. because knowing it rolled over would only help some of
the time and eventually GetTickCount64 will roll over as well, and then I
have the same problem.. just not as often..  and eventually GetTickCount64
will be so much larger than GetTickCount, I can never know how many times
GetTickCount rolled over.   On top of all this, I need to allow for the
possibility that there was no user input for more than 25 days.. which is
quite likely.   What would be MUCH better would be a TDateTime variable that
gets updated during user input and stops getting updated when there is no
user input... but I don't think there is such a thing... or is there?

Is there a way to reset the tick count to 0?   If I reset it to 0 when I
knew there was user input, 
If there is, is it a bad idea to reset it to 0?  Maybe some system processes
are also using it?

Anyone have any ideas?  I don't need this accurate to 1ms,  accurate to a
second or ten would be fine.

James



More information about the fpc-pascal mailing list