检测鼠标键盘多久没有活动(使用GetLastInputInfo API函数检测)

时间:2021-03-14 03:49:57

DELPHI代码

  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, ExtCtrls;
  6. type
  7. TForm1 = class(TForm)
  8. Button1: TButton;
  9. Timer1: TTimer;
  10. procedure Timer1Timer(Sender: TObject);
  11. private
  12. { Private declarations }
  13. public
  14. { Public declarations }
  15. end;
  16. //typedef struct tagLASTINPUTINFO {
  17. //UINT cbSize;
  18. // DWORD dwTime;
  19. // LASTINPUTINFO, *PLASTINPUTINFO;
  20. type
  21. LASTINPUTINFO = record
  22. cbSize:UINT;
  23. dwTime:DWORD;
  24. end;
  25. var
  26. Form1: TForm1;
  27. implementation
  28. {$R *.dfm}
  29. function GetInputAwayTime():DWORD;
  30. var
  31. lpi:TLastInputInfo;
  32. begin
  33. lpi.cbSize := sizeof(lpi);
  34. GetLastInputInfo(lpi);
  35. Result := Round((GetTickCount()-lpi.dwTime)/1000);
  36. end;
  37. procedure TForm1.Timer1Timer(Sender: TObject);
  38. begin
  39. Caption := IntToStr(GetInputAwayTime)
  40. end;
  41. end.

VC代码

    1. DWORD GetInputAwayTime()
    2. {
    3. LASTINPUTINFO lpi;
    4. lpi.cbSize = sizeof(lpi);
    5. GetLastInputInfo(&lpi);
    6. return DWORD((GetTickCount()-lpi.dwTime)/1000);
    7. }

http://blog.csdn.net/cmdasm/article/details/10158601