windows键盘勾子的使用

来源:互联网 发布:网络歌手好听的歌2016 编辑:程序博客网 时间:2024/06/05 18:31

SetWindowsHookEx 原型如下

 

 HHOOKSetWindowsHookEx(

 int idHook,        // 安装勾子的类型 键盘勾子=WH_KEYBOARD

 HOOKPROC lpfn,     // KeyboardProc回调函数的地址  最重要的参数就是这个了

 HINSTANCE hMod,    // 函有回调函数动态链接库的句柄 一般用LoadLibrary来获取

                     //也可以用GetModuleHandle获取  histance

 DWORD dwThreadId  // 要安装勾子的线程IDGetWindowThreadProcessId,

                     // (可以为0即全局勾子,将DLL注入到所有进程的线程)

);

 回调函数格式

 LRESULT CALLBACK KeyboardProc(

 int code,       // 这个值=HC_ACTION=0表示在wParam且lParam参数包含有关击键信息。

                  //假如这个值小于0(HC_ACTION)则回调函数返回值必需=CallNextHookEx返回值

                  //(也就是说必须调用一次CallNextHookEx,并把它的返回值给传给回调函数

 WPARAM wParam,  // 虚拟键码

 LPARAM lParam   // 击键信息资料 这个数的31位(bit)为0表示键按下,为1表示键释放

);

 

 例子:  

    h:=FindWindow(nil,'Element Client'); 

    GameTid:=GetWindowThreadProcessId(h);

   SetWindowsHookEx(WH_KEYBOARD,@Keyproc,GetModuleHandle('???.dll'),GameTid);



Function keyproc(icode,wp,lp:integer):DWORD;stdcall;   //键盘HOOK回调函数
begin
  if (icode=HC_ACTION) then
            begin
              if (wp=VK_HOME)and ((1 shl 31)and lp=0) then
              begin
             // MessageBox(0,'显示外挂','显示外挂',0);
             if form1=nil then  Form1:=Tform1.Create(nil);
                form1.Visible:=not form1.Visible;
              end;
            end;
 keyProc:=CallNextHookEx(keyhhk,icode,wp,lp);
end;
Function installKeyProc():boolean;stdcall;
var
 h:HWND;
 GameTid:THandle;
begin
    Result:=false;
    h:=FindWindow(nil,'Element Client');
    if h=0 then begin Messagebox(0,'未找到游戏','error',0);exit; end;//如果游戏未打开则退出
    GameTid:=GetWindowThreadProcessId(h);
    keyhhk:=SetWindowsHookEx(WH_KEYBOARD,@Keyproc,GetModuleHandle('dllGame.dll'),GameTid);
    if keyhhk>0 then Result:=true;
end;
procedure DllEnterProc(reason:integer);
begin
   case reason of
   windows.DLL_PROCESS_ATTACH: begin end;
   windows.DLL_PROCESS_DETACH: begin Form1.Free;form1:=nil; end;
   end;
end;
exports   //导出函数
  add,
  installKeyProc;


begin
//Messagebox(0,'Loading','error',0);
dllProc:=@DllEnterProc;

0 0
原创粉丝点击