您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > VB2010VBNET源码 HOOK 键盘钩子
'***************************'模块代码Module1'***************************OptionExplicitPrivateDeclareFunctionCallNextHookExLibuser32_(ByValhHookAsLong,_ByValnCodeAsLong,_ByValwParamAsLong,_lParamAsAny)AsLongPrivateDeclareFunctionSetWindowsHookExLibuser32_AliasSetWindowsHookExA_(ByValidHookAsLong,_ByVallpfnAsLong,_ByValhmodAsLong,_ByValdwThreadIdAsLong)AsLongPrivateDeclareFunctionUnhookWindowsHookExLibuser32_(ByValhHookAsLong)AsLongPrivateDeclareSubCopyMemoryLibkernel32_AliasRtlMoveMemory_(DestinationAsAny,_SourceAsAny,_ByValLengthAsLong)PrivateTypePKBDLLHOOKSTRUCTvkCodeAsLongscanCodeAsLongflagsAsLongtimeAsLongdwExtraInfoAsLongEndTypePrivateConstWM_KEYDOWN=&H100PrivateConstWM_SYSKEYDOWN=&H104PrivateConstWM_KEYUP=&H101PrivateConstWM_SYSKEYUP=&H105PrivateConstVK_LWIN=&H5BPrivateConstVK_RWIN=&H5CPrivateConstHC_ACTION=0PrivateConstWH_KEYBOARD_LL=13PrivateConstVK_CONTROL=&H11PrivateConstVK_ESCAPE=&H1BPrivateConstVK_MENU=&H12PrivateConstVK_TAB=&H9PrivateConstVK_Delete=&H2EPrivatelngHookAsLong'使用底层KeyboardHook拦截按键消息PublicFunctionLowLevelKeyboardProc(ByValnCodeAsLong,_ByValwParamAsLong,_ByVallParamAsLong)AsLongDimblnHookAsBooleanDimpAsPKBDLLHOOKSTRUCTIfnCode=HC_ACTIONThenSelectCasewParamCaseWM_KEYDOWN,WM_SYSKEYDOWN,WM_KEYUP,WM_SYSKEYUPCallCopyMemory(p,ByVallParam,Len(p))Ifp.vkCode=VK_LWINOrp.vkCode=VK_RWINThenblnHook=True'按下了左/右Win键Ifp.vkCode=VK_CONTROLOrp.vkCode=VK_ESCAPEThenblnHook=True'按下了Ctrl+Esc键Ifp.vkCode=VK_MENUOrp.vkCode=VK_TABThenblnHook=True'按下了Alt+Tab键CaseElse'donothingEndSelectEndIfIfblnHookThenLowLevelKeyboardProc=1ElseCallCallNextHookEx(WH_KEYBOARD_LL,nCode,wParam,lParam)EndIfEndFunctionPublicSubHooK()lngHook=SetWindowsHookEx(WH_KEYBOARD_LL,_AddressOfLowLevelKeyboardProc,_App.hInstance,_0)EndSubPublicSubUnHooK()CallUnhookWindowsHookEx(lngHook)EndSub'***************************'窗体代码Form1'***************************PrivateSubForm_Load()CallHooK'屏蔽Win键EndSubPrivateSubForm_Unload(CancelAsInteger)CallUnHooK'取消屏蔽Win键EndSub这只是简单的屏蔽了几个键,当然如果要屏蔽更多的键,只要在模块的LowLevelKeyboardProc函数中设置你想要拦截键盘键值过滤掉就可以了。
本文标题:VB2010VBNET源码 HOOK 键盘钩子
链接地址:https://www.777doc.com/doc-3259478 .html