|
楼主 |
发表于 2024-2-10 12:33
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Public hHook As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long)
Const WH_KEYBOARD = 2
Const WM_KEYDOWN = &H100
Public a As Long
Public Type EVENTMSG
vKey As Long
sKey As Long
flag As Long
time As Long
End Type
Public mymsg As EVENTMSG
Public Const WH_KEYBOARD_LL = 2
Sub BeginHK()
'获取当前的线程ID
I = GetCurrentThreadId
'这里安装的是键盘钩子
hHook = SetWindowsHookEx(2, AddressOf HookProc, 0, I)
End Sub
'Hook程序
Public Function HookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'如果code参数<0,则一定要返回CallNextHookEx函数的返回值
Dim sKeyName As String
sKeyName = Space(256)
bLen = 256
If code < 0 Then
HookProc = CallNextHookEx(hHook, code, wParam, lParam)
Else
ResultLen = GetKeyNameText(lParam, sKeyName, bLen)
If Left(sKeyName, ResultLen) = "1" Then
Debug.Print "你准确找到了" & Left(sKeyName, ResultLen) & "键"
End If
HookProc = 1
End If
End Function
Sub EndHK()
UnhookWindowsHookEx hHook
End Sub
|
|