我的目的是写一个系统钩子(全局的),便现在发现只有激活调用程序(调用程序获取到焦点)才捉到事件,请问是为什么?

解决方案 »

  1.   

    library hook;uses
      SysUtils,
      Classes,
      HookUnit in 'HookUnit.pas';
    exports
      EnableHotKeyHook,
      DisableHotKeyHook;
    {$R *.res}begin
      hNextHookProc := 0;
      procSaveExit := ExitProc;
      ExitProc := @HotKeyHookExit;
      
      MouseNextProc:=0;
      MouseSaveExit:=ExitProc;
      ExitProc:=@MouseHookExit;
    end.
    /////////////////////////////////////////////////////////////////////////
    unit HookUnit;interface
    uses Windows, Messages,Dialogs,SysUtils,registry,DateUtils;var
      hNextHookProc: HHook; //保存SetWindowsHookEx的返回值
      procSaveExit: Pointer;
      LastTime:string;  MouseNextProc:HHook;
      MouseSaveExit:Pointer;
    function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
    procedure HotKeyHookExit; far;function MouseHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
    procedure MouseHookExit; far;procedure WriteReg;function EnableHotKeyHook: BOOL; stdcall; //安装钩子
    function DisableHotKeyHook: BOOL; stdcall; //卸载钩子implementationprocedure WriteReg;
    var
      reg: TRegistry;
    begin
      //每十秒写一次
      if abs(SecondsBetween(now, strtodatetime(LastTime)))>10 then
      begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_LOCAL_MACHINE;
        if reg.OpenKey('SOFTWARE\BSQs', true) then
          reg.WriteString('PointTime', datetimetostr(now));
        reg.CloseKey;
        reg.Free;
        LastTime:=datetimetostr(now);
      end;
    end;function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;export;begin
      try
        WriteReg;
        Result:=CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
      except
        Result:=CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
      end;
    end;function MouseHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;export;
    begin
      try
        WriteReg;
        Result:=CallNextHookEx(MouseNextProc, iCode, wParam, lParam);
      except
        Result:=CallNextHookEx(MouseNextProc, iCode, wParam, lParam);
      end;
    end;function EnableHotKeyHook: BOOL;
    begin
      try
        Result := False; //初始化返回值
        LastTime:=datetimetostr(now);
        //注册hook
        if not hNextHookProc <> 0 then //如果没有注册,挂上键盘钩子,同时传回值必须保留下来,免得Hook呼叫链结断掉
          hNextHookProc := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookHandler, HInstance, 0);
        if not MouseNextProc <>0 then
          MouseNextProc:= SetWindowsHookEx(WH_MOUSE, MouseHookHandler, HInstance, 0);
        Result := MouseNextProc <> 0; //通过返回值确定是否注册成功
      except  end;
    end;function DisableHotKeyHook: BOOL;
    begin
      try
        if MouseNextProc <> 0 then
        begin
          UnhookWindowshookEx(hNextHookProc); //解除Keyboard Hook
          hNextHookProc := 0; //恢复标志      UnhookWindowshookEx(MouseNextProc);
          MouseNextProc:=0;
        end;
        Result := MouseNextProc = 0; //返回是否注销成功
      except  end;
    end;procedure HotKeyHookExit;
    begin
      if MouseNextProc <> 0 then DisableHotKeyHook; //如果忘了解除HOOK,自动代理解除动作
      ExitProc := MouseSaveExit;
    end;procedure MouseHookExit;
    begin
      if MouseNextProc<>0 then  DisableHotKeyHook;
      ExitProc :=MouseSaveExit;
    end;
    end.