How to detect inactive user

后端 未结 4 682
醉梦人生
醉梦人生 2020-12-12 18:09

How to detect inactive (idle) user in Windows application? I\'d like to shutdown application when there hasn\'t been any input (keyboard, mouse) from user for certain period

4条回答
  •  轮回少年
    2020-12-12 18:52

    To track a user's idle time you could hook keyboard and mouse activity. Note, however, that installing a system-wide message hook is a very invasive thing to do and should be avoided if possible, since it will require your hook DLL to be loaded into all processes.

    Another solution is to use the GetLastInputInfo API function (if your application is running on Win2000 (and up) machines). GetLastInputInfo retrieves the time (in milliseconds) of the last input event (when the last detected user activity has been received, be it from keyboard or mouse).

    Here's a simple example. The SecondsIdle function returns a number of second with no user activity (called in an OnTimer event of a TTimer component).

    ~~~~~~~~~~~~~~~~~~~~~~~~~
    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;
    

    http://delphi.about.com/od/adptips2004/a/bltip1104_4.htm

提交回复
热议问题