2022年用VB实现的全局键盘钩子 .pdf

上传人:Q****o 文档编号:27521068 上传时间:2022-07-25 格式:PDF 页数:7 大小:44.58KB
返回 下载 相关 举报
2022年用VB实现的全局键盘钩子 .pdf_第1页
第1页 / 共7页
2022年用VB实现的全局键盘钩子 .pdf_第2页
第2页 / 共7页
点击查看更多>>
资源描述

《2022年用VB实现的全局键盘钩子 .pdf》由会员分享,可在线阅读,更多相关《2022年用VB实现的全局键盘钩子 .pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、用 VB实现的全局键盘钩子2010-04-06 13:30 代码功能:实时监测Caps Lock、NumLock 、Scroll Lock 三个按件的状态,并显示在 Label1 Label2 Label3三个标签中.bas模块中Public m_hDllKbdHook As Long public variable holding the handle to the hook procedure Public Const WH_KEYBOARD_LL As Long = 13 enables monitoring of keyboard input events about to be po

2、sted in a thread input queue Private Const HC_ACTION As Long = 0 wParam and lParam parameters contain information about a keyboard message Public Const VK_CAPITAL As Long = &H14 Public Const VK_NUMLOCK As Long = &H90 Public Const VK_SCROLL As Long = &H91 Private Const LLKHF_UP As Long = &H80& test t

3、he transition-state flag Public Type KeyboardBytes kbByte(0 To 255) As Byte End Type Private Type KBDLLHOOKSTRUCT vkCode As Long a virtual-key code in the range 1 to 254 scanCode As Long hardware scan code for the key flags As Long specifies the extended-key flag, event-injected flag, context code,

4、and transition-state flag time As Long time stamp for this message dwExtraInfo As Long extra info associated with the message End Type Public Declare Function SetWindowsHookEx Lib user32 _ Alias SetWindowsHookExA _ (ByVal idHook As Long, _ ByVal lpfn As Long, _ ByVal hmod As Long, _ ByVal dwThreadId

5、 As Long) As Long 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - Public Declare Function UnhookWindowsHookEx Lib user32 _ (ByVal hHook As Long) As Long Public Declare Function CallNextHookEx Lib user32 _ (ByVal hHook As Long, _ ByVal nCode

6、 As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Public Declare Sub CopyMemory Lib kernel32 _ Alias RtlMoveMemory _ (pDest As Any, _ pSource As Any, _ ByVal cb As Long) Public Declare Function GetKeyboardState Lib user32 _ (kbArray As KeyboardBytes) As Long Public Declare Function G

7、etKeyState Lib user32 _ (ByVal nVirtKey As Long) As Integer Public Function LowLevelKeyboardProc(ByVal nCode As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Dim kbdllhs As KBDLLHOOKSTRUCT If nCode = HC_ACTION Then Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs) If (kbdllhs.flags

8、 And LLKHF_UP) Then Select Case kbdllhs.vkCode Case VK_NUMLOCK Form1.Label1.Visible = (GetKeyState(VK_NUMLOCK) = &HFF81) Case VK_CAPITAL Form1.Label2.Visible = (GetKeyState(VK_CAPITAL) = &HFF81) Case VK_SCROLL 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页

9、- - - - - - - - - Form1.Label3.Visible = (GetKeyState(VK_SCROLL) = &HFF81) Case Else End Select End If End If nCode = HC_ACTION LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, _ nCode, _ wParam, _ lParam) End Function Form1中加入 3 个标签控件 Label1 、Label2 、Label3 Form1中的代码Private Sub Form_Load() Dim

10、kbdState As KeyboardBytes Call GetKeyboardState(kbdState) With Label1 .Caption = Numlock is ON .Alignment = vbRightJustify End With With Label2 .Caption = Caps lock is ON .Alignment = vbRightJustify End With With Label3 .Caption = Scroll lock is ON .Alignment = vbRightJustify End With Label1.Visible

11、 = kbdState.kbByte(VK_NUMLOCK) = 1 Label2.Visible = kbdState.kbByte(VK_CAPITAL) = 1 Label3.Visible = kbdState.kbByte(VK_SCROLL) = 1 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - set and obtain the handle to the keyboard hook m_hDllKbdHook

12、 = SetWindowsHookEx(WH_KEYBOARD_LL, _ AddressOf LowLevelKeyboardProc, _ App.hInstance, _ 0&) If m_hDllKbdHook = 0 Then MsgBox Failed to install low-level keyboard hook. End If End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) If m_hDllKbdHook 0 Then Call UnhookWindowsHoo

13、kEx(m_hDllKbdHook) End If End Sub 还有一段可以禁用Ctrl+Esc Alt + Esc Alt+Tab三组热键的Private Const WH_KEYBOARD_LL = 13& enables monitoring of keyboard input events about to be posted in a thread input queue Private Const HC_ACTION = 0& wParam and lParam parameters contain information about a keyboard message Pr

14、ivate Const LLKHF_EXTENDED = &H1& test the extended-key flag Private Const LLKHF_INJECTED = &H10& test the event-injected flag Private Const LLKHF_ALTDOWN = &H20& test the context code Private Const LLKHF_UP = &H80& test the transition-state flag Private Const VK_TAB = &H9 virtual key constants Priv

15、ate Const VK_CONTROL = &H11 Private Const VK_ESCAPE = &H1B Private Type KBDLLHOOKSTRUCT vkCode As Long a virtual-key code in the range 1 to 254 scanCode As Long hardware scan code for the key 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - -

16、flags As Long specifies the extended-key flag, event-injected flag, context code, and transition-state flag time As Long time stamp for this message dwExtraInfo As Long extra info associated with the message End Type Private Declare Function SetWindowsHookEx Lib user32 _ Alias SetWindowsHookExA _ (B

17、yVal idHook As Long, _ ByVal lpfn As Long, _ ByVal hmod As Long, _ ByVal dwThreadId As Long) As Long Private Declare Function UnhookWindowsHookEx Lib user32 _ (ByVal hHook As Long) As Long Private Declare Function CallNextHookEx Lib user32 _ (ByVal hHook As Long, _ ByVal nCode As Long, _ ByVal wPara

18、m As Long, _ ByVal lParam As Long) As Long Private Declare Sub CopyMemory Lib kernel32 _ Alias RtlMoveMemory _ (pDest As Any, _ pSource As Any, _ ByVal cb As Long) Private Declare Function GetAsyncKeyState Lib user32 _ (ByVal vKey As Long) As Integer Private m_hDllKbdHook As Long private variable ho

19、lding the handle to the hook procedure Public Sub Main() set and obtain the handle to the keyboard hook m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, _ AddressOf LowLevelKeyboardProc, _ App.hInstance, _ 0&) If m_hDllKbdHook 0 Then MsgBox Ctrl+Esc, Alt+Tab and Alt+Esc are blocked. & _ 名师资料总结 - - -

20、精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - Click OK to quit and re-enable the keys., _ vbOKOnly Or vbInformation, _ Keyboard Hook Active Call UnhookWindowsHookEx(m_hDllKbdHook) Else MsgBox Failed to install low-level keyboard hook - & Err.LastDllE

21、rror End If End Sub Public Function LowLevelKeyboardProc(ByVal nCode As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Static kbdllhs As KBDLLHOOKSTRUCT If nCode = HC_ACTION Then Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs) Ctrl+Esc - If (kbdllhs.vkCode = VK_ESCAPE) And _ CBool

22、(GetAsyncKeyState(VK_CONTROL) _ And &H8000) Then Debug.Print Ctrl+Esc blocked LowLevelKeyboardProc = 1 Exit Function End If kbdllhs.vkCode = VK_ESCAPE Alt+Tab - If (kbdllhs.vkCode = VK_TAB) And _ CBool(kbdllhs.flags And _ LLKHF_ALTDOWN) Then 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名

23、师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - Debug.Print Alt+Tab blocked LowLevelKeyboardProc = 1 Exit Function End If kbdllhs.vkCode = VK_TAB Alt+Esc - If (kbdllhs.vkCode = VK_ESCAPE) And _ CBool(kbdllhs.flags And _ LLKHF_ALTDOWN) Then Debug.Print Alt+Esc blocked LowLevelKeyboardProc = 1 Exit Function End If kbdllhs.vkCode = VK_ESCAPE End If nCode = HC_ACTION LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, _ nCode, _ wParam, _ lParam) End Function 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁