vb6钩子问题。大哥哥大姐姐们帮帮忙啊
Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongPrivate Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As LongPrivate Declare Function GetKeyboardState Lib "user32 " (pbKeyState As Byte) As LongPrivate Declare Function GetKeyState Lib "user32" (ByVal nVirkey As Long) As LongPrivate Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long'Private Declare Sub CopyMemory1 Lib "kernel32" Alias "RtlMoveMemory" (lpvdest As Any, ByVal lpsource As Long, ByVal cbcopy As Long)Private JPhHook As LongPrivate Declare Function GetActiveWindow Lib "user32" () As LongPrivate Type KEYMSGS vKey As Long sKey As Long flag As Long time As LongEnd TypePrivate keyMsg As KEYMSGS'消息Private Const WH_KEYBOARD_LL As Long = 13Private Const HC_ACTION = 0Private Const HC_SYSMODALOFF = 5Private Const HC_SYSMODALON = 4'键盘消息Private Const WM_KEYDOWN = &H100Private Const WM_KEYUP = &H101Private Const WM_SYSKEYDOWN = &H104Private Const WM_SYSKEYUP = &H105Public Sub EnableHook() If JPhHook = 0 Then JPhHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf JPHookProc, App.hInstance, 0) End IfEnd SubPublic Sub FreeHook() If JPhHook <> 0 Then Call UnhookWindowsHookEx(JPhHook) End IfEnd SubPublic Function JPHookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim IKey As Long Dim strKeyName As String * 255 Dim strLen As Long If nCode = HC_ACTION Then CopyMemory keyMsg, lParam, LenB(keyMsg) Select Case wParam Case WM_SYSKEYDOWN, WM_KEYDOWN, WM_SYSKEYUP, WM_KEYUP: IKey = keyMsg.sKey And &HFF IKey = IKey * 65536 strLen = GetKeyNameText(IKey, strKeyName, strLen) Debug.Print "键名:" & Left(strKeyName, strLen) Debug.Print "虚拟:" & Left(keyMsg.vKey And &HFF, "0") Debug.Print "扫描码:" & Format(IKey / 65536, "0") End Select End IfEnd Function
Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongPrivate Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As LongPrivate Declare Function GetKeyboardState Lib "user32 " (pbKeyState As Byte) As LongPrivate Declare Function GetKeyState Lib "user32" (ByVal nVirkey As Long) As LongPrivate Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long'Private Declare Sub CopyMemory1 Lib "kernel32" Alias "RtlMoveMemory" (lpvdest As Any, ByVal lpsource As Long, ByVal cbcopy As Long)Private JPhHook As LongPrivate Declare Function GetActiveWindow Lib "user32" () As LongPrivate Type KEYMSGS vKey As Long sKey As Long flag As Long time As LongEnd TypePrivate keyMsg As KEYMSGS'消息Private Const WH_KEYBOARD_LL As Long = 13Private Const HC_ACTION = 0Private Const HC_SYSMODALOFF = 5Private Const HC_SYSMODALON = 4'键盘消息Private Const WM_KEYDOWN = &H100Private Const WM_KEYUP = &H101Private Const WM_SYSKEYDOWN = &H104Private Const WM_SYSKEYUP = &H105Public Sub EnableHook() If JPhHook = 0 Then JPhHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf JPHookProc, App.hInstance, 0) End IfEnd SubPublic Sub FreeHook() If JPhHook <> 0 Then Call UnhookWindowsHookEx(JPhHook) End IfEnd SubPublic Function JPHookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim IKey As Long Dim strKeyName As String * 255 Dim strLen As Long If nCode = HC_ACTION Then CopyMemory ByVal keyMsg, ByVal lParam, Len(keyMsg) Select Case wParam Case WM_SYSKEYDOWN, WM_KEYDOWN, WM_SYSKEYUP, WM_KEYUP: IKey = keyMsg.sKey And &HFF IKey = IKey * 65536 strLen = 256 strLen = GetKeyNameText(IKey, strKeyName, strLen) Debug.Print "键名:" & Left(strKeyName, InStr(strKeyName, Chr(0)) - 1) Debug.Print "虚拟:" & keyMsg.vKey Debug.Print "扫描码:" & Format(IKey / 65536, "0") End Select End IfEnd Function