问题描述
这段键盘钩子,在窗口里怎么调用啊?ImportsSystem.Reflection,System.Threading,System.ComponentModel,System.Runtime.InteropServices'''<summary>本类可以在.NET环境下使用系统键盘与鼠标钩子</summary>PublicClassSystemHook定义结构API声明导入常量声明事件委托处理PrivatehMouseHookAsIntegerPrivatehKeyboardHookAsIntegerPrivateSharedMouseHookProcedureAsHookProcPrivateSharedKeyboardHookProcedureAsHookProc创建与析构类型'''<summary>开始安装系统钩子</summary>'''<paramname="InstallKeyboardHook">挂钩键盘消息</param>'''<paramname="InstallMouseHook">挂钩鼠标消息</param>PublicSubStartHook(OptionalByValInstallKeyboardHookAsBoolean=True,OptionalByValInstallMouseHookAsBoolean=False)'注册键盘钩子IfInstallKeyboardHookAndAlsohKeyboardHook=0ThenKeyboardHookProcedure=NewHookProc(AddressOfKeyboardHookProc)hKeyboardHook=SetWindowsHookEx(WH_KEYBOARD_LL,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)),0)IfhKeyboardHook=0Then'检测是否注册完成UnHook(True,False)'在这里反注册ThrowNewWin32Exception(Marshal.GetLastWin32Error)'报告错误EndIfEndIf'注册鼠标钩子IfInstallMouseHookAndAlsohMouseHook=0ThenMouseHookProcedure=NewHookProc(AddressOfMouseHookProc)hMouseHook=SetWindowsHookEx(WH_MOUSE_LL,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)),0)IfhMouseHook=0ThenUnHook(False,True)ThrowNewWin32Exception(Marshal.GetLastWin32Error)EndIfEndIfEndSub'''<summary>立刻卸载系统钩子</summary>'''<paramname="UninstallKeyboardHook">卸载键盘钩子</param>'''<paramname="UninstallMouseHook">卸载鼠标钩子</param>'''<paramname="ThrowExceptions">是否报告错误</param>PublicSubUnHook(OptionalByValUninstallKeyboardHookAsBoolean=True,OptionalByValUninstallMouseHookAsBoolean=True,OptionalByValThrowExceptionsAsBoolean=False)'卸载键盘钩子IfhKeyboardHook<>0AndAlsoUninstallKeyboardHookThenDimretKeyboardAsInteger=UnhookWindowsHookEx(hKeyboardHook)hKeyboardHook=0IfThrowExceptionsAndAlsoretKeyboard=0Then'如果出现错误,是否报告错误ThrowNewWin32Exception(Marshal.GetLastWin32Error)'报告错误EndIfEndIf'卸载鼠标钩子IfhMouseHook<>0AndAlsoUninstallMouseHookThenDimretMouseAsInteger=UnhookWindowsHookEx(hMouseHook)hMouseHook=0IfThrowExceptionsAndAlsoretMouse=0ThenThrowNewWin32Exception(Marshal.GetLastWin32Error)EndIfEndIfEndSub'键盘消息的委托处理代码PrivateFunctionKeyboardHookProc(ByValnCodeAsInteger,ByValwParamAsInteger,ByVallParamAsIntPtr)AsIntegerStatichandledAsBoolean:handled=FalseIfnCode>=0AndAlso(events("KeyDown")IsNotNothingOrElseevents("KeyPress")IsNotNothingOrElseevents("KeyUp")IsNotNothing)ThenStaticMyKeyboardHookStructAsKeyboardHookStructMyKeyboardHookStruct=DirectCast(Marshal.PtrToStructure(lParam,GetType(KeyboardHookStruct)),KeyboardHookStruct)'激活KeyDownIfwParam=WM_KEYDOWNOrElsewParam=WM_SYSKEYDOWNThen'如果消息为按下普通键或系统键DimeAsNewKeyEventArgs(MyKeyboardHookStruct.vkCode)RaiseEventKeyDown(Me,e)'激活事件handled=handledOre.Handled'是否取消下一个钩子EndIf'激活KeyUpIfwParam=WM_KEYUPOrElsewParam=WM_SYSKEYUPThenDimeAsNewKeyEventArgs(MyKeyboardHookStruct.vkCode)RaiseEventKeyUp(Me,e)handled=handledOre.HandledEndIf'激活KeyPress(TODO:此段代码还有BUG!)IfwParam=WM_KEYDOWNThenDimisDownShiftAsBoolean=(GetKeyState(VK_SHIFT)&&H80=&H80)DimisDownCapslockAsBoolean=(GetKeyState(VK_CAPITAL)<>0)DimkeyState(256)AsByteGetKeyboardState(keyState)DiminBuffer(2)AsByteIfToAscii(MyKeyboardHookStruct.vkCode,MyKeyboardHookStruct.ScanCode,keyState,inBuffer,MyKeyboardHookStruct.Flags)=1ThenStatickeyAsChar:key=Chr(inBuffer(0))'BUG所在'IfisDownCapslockXorisDownShiftAndChar.IsLetter(key)Then'key=Char.ToUpper(key)'EndIfDimeAsNewKeyPressEventArgs(key)RaiseEventKeyPress(Me,e)handled=handledOre.HandledEndIfEndIf'取消或者激活下一个钩子IfhandledThenReturn1ElseReturnCallNextHookEx(hKeyboardHook,nCode,wParam,lParam)EndIfEndFunction'鼠标消息的委托处理代码PrivateFunctionMouseHookProc(ByValnCodeAsInteger,ByValwParamAsInteger,ByVallParamAsIntPtr)AsIntegerIfnCode>=0AndAlsoevents("MouseActivity")IsNotNothingThenStaticmouseHookStructAsMouseLLHookStructmouseHookStruct=DirectCast(Marshal.PtrToStructure(lParam,GetType(MouseLLHookStruct)),MouseLLHookStruct)StaticmoubutAsMouseButtons:moubut=MouseButtons.None'鼠标按键StaticmouseDeltaAsInteger:mouseDelta=0'滚轮值SelectCasewParamCaseWM_LBUTTONDOWNmoubut=MouseButtons.LeftCaseWM_RBUTTONDOWNmoubut=MouseButtons.RightCaseWM_MBUTTONDOWNmoubut=MouseButtons.MiddleCaseWM_MOUSEWHEELStaticintAsInteger:int=(mouseHookStruct.MouseData>>16)And&HFFFF'本段代码CLE添加,模仿C#的Short从Int弃位转换Ifint>Short.MaxValueThenmouseDelta=int-65536ElsemouseDelta=intEndSelectStaticclickCountAsInteger:clickCount=0'单击次数Ifmoubut<>MouseButtons.NoneThenIfwParam=WM_LBUTTONDBLCLKOrElsewParam=WM_RBUTTONDBLCLKOrElsewParam=WM_MBUTTONDBLCLKThenclickCount=2ElseclickCount=1EndIfEndIfDimeAsNewMouseEventArgs(moubut,clickCount,mouseHookStruct.PT.X,mouseHookStruct.PT.Y,mouseDelta)RaiseEventMouseActivity(Me,e)EndIfReturnCallNextHookEx(hMouseHook,nCode,wParam,lParam)'激活下一个钩子EndFunction'''<summary>键盘钩子是否有效</summary>PublicPropertyKeyHookEnabled()AsBooleanGetReturnhKeyboardHook<>0EndGetSet(ByValvalueAsBoolean)IfvalueThenStartHook(True,False)ElseUnHook(True,False)EndSetEndProperty'''<summary>鼠标钩子是否有效</summary>PublicPropertyMouseHookEnabled()AsBooleanGetReturnhMouseHook<>0EndGetSet(ByValvalueAsBoolean)IfvalueThenStartHook(False,True)ElseUnHook(False,True)EndSetEndPropertyEndClass
解决方案
解决方案二:
如果你的代码是好用的。在程序运行的时候调用StartHook在程序退出的时候调用UnHook然后处理KeyPressMouseMove这些事件即可。
解决方案三:
引用1楼caozhy的回复:
如果你的代码是好用的。在程序运行的时候调用StartHook在程序退出的时候调用UnHook然后处理KeyPressMouseMove这些事件即可。