2008. április 4., péntek

How to check when the user last clicked on the program's interface


Problem/Question/Abstract:

Is there a way to find out when the user last clicked on a program's interface? It is some sort of like idle time but the idle time for this specific program.

Answer:

From inside the application it is fairly easy. You need three pieces of equipment here:

A "Time of last activity" variable, field of your main form

FLastActive: TDateTime;


A timer that regularly checks the FLastActive variable against the current time. Set it to an interval of, say 60000, and set its Active property to true at design-time. The OnTimer event handler would be something like this (timeout after 15 minutes):

if (FLastActive + EncodeTime(0, 15, 0, 0)) < Now then
  Close;


A handler for the Application.OnMessage event that updates the FLastActive variable on each key or mouse message. The handler would do something like this:

case msg.Message of
  WM_KEYFIRST..WM_KEYLAST, WM_MOUSEFIRST..WM_MOUSELAST:
    FLastActive := Now;
end;

Nincsenek megjegyzések:

Megjegyzés küldése