M
man8888
Unregistered / Unconfirmed
GUEST, unregistred user!
这是个捕获鼠标键盘是否有输入的类,但是鼠标钩子函数MouseHookCallBack总也不起作用,不知道喂虾米???。。
unit IdleTimer;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TIdleTimer = class(TTimer)
private
function GetSnooze: DWORD;
procedure SetSnooze(const Value: DWORD);
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Snooze: DWORD read GetSnooze write SetSnooze;
end;
implementation
var
Instances: integer;
CurrentTime: DWORD;
whKeyBoard, whMouse:HHook;
{ TIdleTimer }
function MouseHookCallBack(Code:integer; Msg:lParam; MouseHook:wParam): DWORD;stdcall;
begin
if Code >= 0 then
CurrentTime := GetTickCount;
Result := CallNextHookEx(whMouse, Code, Msg, MouseHook);
end;
function KeyBoardCallBack(Code:integer; Msg:word; KeyBoardHook:Longint): LongInt;stdcall;
begin
if Code >= 0 then
CurrentTime := GetTickCount;
Result := CallNextHookEx(whKeyBoard, Code, Msg, KeyBoardHook);
end;
constructor TIdleTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Inc(Instances);
if Instances =1 then
begin
CurrentTime :=GetTickCount;
whMouse := SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,HInstance,0);
whKeyBoard :=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardCallBack,HInstance,0);
end;
end;
destructor TIdleTimer.Destroy;
begin
Dec(Instances);
if Instances =0 then
begin
UnhookWindowsHookEx(whKeyBoard);
UnhookWindowsHookEx(whMouse);
end;
inherited;
end;
function TIdleTimer.GetSnooze: DWORD;
begin
Result:= GetTickCount - CurrentTime;
end;
procedure TIdleTimer.SetSnooze(const Value: DWORD);
begin
CurrentTime := GetTickCount + Value;
end;
end.
//==================================================
测试代码:
var
Form1: TForm1;
IdleTimer: TIdleTimer;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
IdleTimer:= TIdleTimer.Create(Self);
IdleTimer.Interval:= 100;
IdleTimer.OnTimer:= MyOnTimer;
IdleTimer.Enabled:= true;
end;
procedure TForm1.MyOnTimer(Sender: TObject);
begin
Edit1.Text:= IntToStr( IdleTimer.Snooze );
end;
unit IdleTimer;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TIdleTimer = class(TTimer)
private
function GetSnooze: DWORD;
procedure SetSnooze(const Value: DWORD);
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Snooze: DWORD read GetSnooze write SetSnooze;
end;
implementation
var
Instances: integer;
CurrentTime: DWORD;
whKeyBoard, whMouse:HHook;
{ TIdleTimer }
function MouseHookCallBack(Code:integer; Msg:lParam; MouseHook:wParam): DWORD;stdcall;
begin
if Code >= 0 then
CurrentTime := GetTickCount;
Result := CallNextHookEx(whMouse, Code, Msg, MouseHook);
end;
function KeyBoardCallBack(Code:integer; Msg:word; KeyBoardHook:Longint): LongInt;stdcall;
begin
if Code >= 0 then
CurrentTime := GetTickCount;
Result := CallNextHookEx(whKeyBoard, Code, Msg, KeyBoardHook);
end;
constructor TIdleTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Inc(Instances);
if Instances =1 then
begin
CurrentTime :=GetTickCount;
whMouse := SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,HInstance,0);
whKeyBoard :=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardCallBack,HInstance,0);
end;
end;
destructor TIdleTimer.Destroy;
begin
Dec(Instances);
if Instances =0 then
begin
UnhookWindowsHookEx(whKeyBoard);
UnhookWindowsHookEx(whMouse);
end;
inherited;
end;
function TIdleTimer.GetSnooze: DWORD;
begin
Result:= GetTickCount - CurrentTime;
end;
procedure TIdleTimer.SetSnooze(const Value: DWORD);
begin
CurrentTime := GetTickCount + Value;
end;
end.
//==================================================
测试代码:
var
Form1: TForm1;
IdleTimer: TIdleTimer;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
IdleTimer:= TIdleTimer.Create(Self);
IdleTimer.Interval:= 100;
IdleTimer.OnTimer:= MyOnTimer;
IdleTimer.Enabled:= true;
end;
procedure TForm1.MyOnTimer(Sender: TObject);
begin
Edit1.Text:= IntToStr( IdleTimer.Snooze );
end;