基于C#实现的HOOK键盘钩子实例代码
本文所述为基于C#实现的HOOK实例,该实例可用来屏蔽系统热键。程序主要实现了安装钩子、传递钩子、卸载钩子等功能。在传递钩子中:
<paramname="pHookHandle">是您自己的钩子函数的句柄。用该句柄可以遍历钩子链</param>
<paramname="nCode">把传入的参数简单传给CallNextHookEx即可</param>
<paramname="wParam">把传入的参数简单传给CallNextHookEx即可</param>,
在HOOK类中定义了一些私有变量:键盘钩子句柄、键盘钩子委托实例、底层的钩子变量等。在钩子捕获消息后,对消息进行处理。
具体实现HOOK代码如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Runtime.InteropServices; usingSystem.Reflection; usingSystem.IO; namespace设置和屏蔽系统热键 { classHOOK { #region私有变量 privateIntPtrm_pKeyboardHook=IntPtr.Zero;///键盘钩子句柄 publicdelegateintHookProc(intnCode,Int32wParam,IntPtrlParam);///钩子委托声明 privateHookProcm_KeyboardHookProcedure;///键盘钩子委托实例 publicconstintidHook=13;///底层的钩子变量 [DllImport("user32.dll",CallingConvention=CallingConvention.StdCall)] publicstaticexternIntPtrSetWindowsHookEx(intidHook,HookProclpfn,IntPtrpInstance,intthreadId);///安装钩子 [DllImport("user32.dll",CallingConvention=CallingConvention.StdCall)]///卸载钩子 publicstaticexternboolUnhookWindowsHookEx(IntPtrpHookHandle); ///传递钩子 ///<paramname="pHookHandle">是您自己的钩子函数的句柄。用该句柄可以遍历钩子链</param> ///<paramname="nCode">把传入的参数简单传给CallNextHookEx即可</param> ///<paramname="wParam">把传入的参数简单传给CallNextHookEx即可</param> ///<paramname="lParam"></param> ///<returns></returns> [DllImport("user32.dll",CallingConvention=CallingConvention.StdCall)] publicstaticexternintCallNextHookEx(IntPtrpHookHandle,intnCode,Int32wParam,IntPtrlParam); [StructLayout(LayoutKind.Sequential)] publicstructKeyMSG { publicintvkCode; publicintscanCode; publicintflags; publicinttime; publicintdwExtraInfo; } protectedconstintWM_QUERYENDSESSION=0x0011; protectedconstintWM_KEYDOWN=0x100; protectedconstintWM_KEYUP=0x101; protectedconstintWM_SYSKEYDOWN=0x104; protectedconstintWM_SYSKEYUP=0x105; protectedconstbyteVK_SHIFT=0x10; protectedconstbyteVK_CAPITAL=0x14; protectedconstbyteVK_NUMLOCK=0x90; protectedconstbyteVK_LSHIFT=0xA0; protectedconstbyteVK_RSHIFT=0xA1; protectedconstintVK_LWIN=91; protectedconstintVK_RWIN=92; protectedconstbyteVK_LCONTROL=0xA2; protectedconstbyteVK_RCONTROL=0x3; protectedconstbyteVK_LALT=0xA4; protectedconstbyteVK_RALT=0xA5; protectedconstbyteLLKHF_ALTDOWN=0x20; publicboolPorwer=true;//是否屏蔽Porwer键 publicstaticintpp=0;//热键的返回值 publicstaticboolisSet=false;//是否设置屏蔽热键,false为设置屏蔽的热键 publicstaticboolisHotkey=false; publicstaticboolisInstall=false;//是否安装钩子,true为安装 #endregion #region事件的声明 publiceventKeyEventHandlerKeyDown;//键盘按下事件 publiceventKeyEventHandlerKeyUp;//键盘松开事件 publiceventKeyPressEventHandlerKeyPress;//键盘单击事件 #endregion #region方法 ///<summary> ///钩子捕获消息后,对消息进行处理 ///</summary> ///<paramnCode="int">标识,键盘是否操作</param> ///<paramwParam="int">键盘的操作值</param> ///<paramlParam="IntPtr">指针</param> privateintKeyboardHookProc(intnCode,intwParam,IntPtrlParam) { if(nCode>-1&&(KeyDown!=null||KeyUp!=null||KeyPress!=null)) { KeyMSGkeyboardHookStruct=(KeyMSG)Marshal.PtrToStructure(lParam,typeof(KeyMSG));//获取钩子的相关信息 KeyEventArgse=newKeyEventArgs((Keys)(keyboardHookStruct.vkCode));//获取KeyEventArgs事件的相磁信息 switch(wParam) { caseWM_KEYDOWN://键盘按下操作 caseWM_SYSKEYDOWN: if(KeyDown!=null)//如果加载了当前事件 { KeyDown(this,e);//调用该事件 } break; caseWM_KEYUP://键盘松开操作 caseWM_SYSKEYUP: if(KeyUp!=null)//如果加载了当前事件 { KeyUp(this,e);//调用该事件 } break; } } returnpp;//是否屏蔽当前热键,1为屏蔽,2为执行 } #endregion #region安装、卸载钩子 ///<summary> ///安装钩子 ///</summary> ///<returns>是否安装成功</returns> publicboolStart() { IntPtrpInstance=(IntPtr)4194304;//钩子所在实例的句柄 if(this.m_pKeyboardHook==IntPtr.Zero)//如果键盘的句柄为空 { this.m_KeyboardHookProcedure=newHookProc(KeyboardHookProc);//声明一个托管钩子 this.m_pKeyboardHook=SetWindowsHookEx(idHook,m_KeyboardHookProcedure,pInstance,0);//安装钩子 if(this.m_pKeyboardHook==IntPtr.Zero)//如果安装失败 { this.Stop();//卸载钩子 returnfalse; } } isInstall=true;//安装了钩子 returntrue; } ///<summary> ///卸载钩子 ///</summary> ///<returns>是否卸载成功</returns> publicboolStop() { if(isInstall==false)//如果没有安装钩子 { returntrue; } boolresult=true; if(this.m_pKeyboardHook!=IntPtr.Zero)//如果安装了钩子 { result=(UnhookWindowsHookEx(this.m_pKeyboardHook)&&result);//卸载钩子 this.m_pKeyboardHook=IntPtr.Zero;//清空键盘的钩子句柄 } returnresult; } #endregion公共方法 } }