Search code examples
delphiwinapidelphi-xe

Automatic shutdown when the program is not used for a certain period of time


We have a program and we are writing in Delphi XE. I have a problem :

If the program is not used for the duration that we set as the parameter, it should automatically return to the input screen. So like windows, there must be a control like mouse and keyboard. Can you help me with this?

Thank you.


Solution

  • Periodically call GetLastInputInfo. This will return the tick count of the last input event. Then compare the difference between that input event's tick count and the current tick count against your timeout.

    The downside of this approach is that you have to deal with the 49 day roll over issue with 32 bit Windows tick counts. As an alternative you might opt to install a system hook to monitor keyboard and mouse events. This will allow you to avoid the 49 day roll over issue, but is considerably more complex to implement.