windows键盘勾子的使用

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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值