Home > Software engineering >  Vb hook doubt
Vb hook doubt

Time:10-31

Recent need to use vb to write a hook procedure, capture all keyboard case information, general methods from the following address: http://blog.sina.com.cn/s/blog_69b6a7c60100uuhf.html
Roughly as follows:
 program source: 
(1) FrmHook source
Option Explicit
Dim WithEvents hooks As ClsHook 'create a Hook for event support for modules ClsHook
Private Declare Function MapVirtualKeyEx Lib "user32" Alias "MapVirtualKeyExA" (ByVal uCode As Long, ByVal uMapType As Long, ByVal DWHKL As Long) As Long
', depending on the type of the specified map to perform different scan code and character conversion
'
'uCode longs to convert the source character or code
'uMapType Long, the control map type, as shown below.
'0 - uCode is a virtual key code? The function returns the corresponding scan code
'1 - uCode is a scan code? The function returns the corresponding virtual key code
'2 - uCode is a virtual key code, the function returns the ASCII value of corresponding (without Shift key combination), key for death, high set to 1, if wrong, return NULL
'DWHKL Long, the keyboard layout handle
Private Declare Function GetKeyboardLayout Lib "user32" (ByVal dwLayout As Long) As Long
'get a handle, describe a given application keyboard layout
'dwLayout,//to check the thread identifier

Private Declare Function GetForegroundWindow Lib "user32 () As" Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal HWND As Long, lpdwProcessId As Long) As Long
'to get linked together with a specified window a process and thread identifier
'lpdwProcessId Long, specify a variable, for loading with the window of a process identifier
'the HWND Long, specifies the window handle
Private Sub Form_Load ()
The Set Hook=New ClsHook
Hook. SetHook
'App. TaskVisible=False
Me. Hide
End Sub
Private Sub Form_Unload (Cancel As Integer)
Hook. UnHook
The Set of Hook=Nothing
End Sub
Private Sub Hook_KeyDown (KeyCode As an Integer, Shift As an Integer) 'hook KeyDown event has occurred, the module of our own definition of event KeyDown
Dim StrCode As String
StrCode=CodeToString (KeyCode)
'judgment Shift
If StrCode="[Shift]" Or StrCode="[Alt] Or StrCode"="[Ctrl]" Then
If Shift=vbAltMask + vbCtrlMask Then StrCode="[Alt + Ctrl]"
If Shift=vbAltMask + vbShiftMask Then StrCode="[Alt + Shift]"
If Shift=vbCtrlMask + vbShiftMask Then StrCode="[Ctrl + Shift]"
If Shift=vbCtrlMask + + vbAltMask vbShiftMask Then StrCode="[Ctrl + Shift + Alt]"

The Else
If the Shift=vbShiftMask Then StrCode=StrCode & amp; "+ [Shift]
"If the Shift=vbCtrlMask Then StrCode=StrCode & amp; "+ [Ctrl]
"If the Shift=vbAltMask Then StrCode=StrCode & amp; "+ [Alt]
"If Shift=vbAltMask + vbCtrlMask Then StrCode=StrCode & amp; "/Alt + Ctrl +"
If Shift=vbAltMask + vbShiftMask Then StrCode=StrCode & amp; "/Alt + Shift +"
If Shift=vbCtrlMask + vbShiftMask Then StrCode=StrCode & amp; "/Ctrl + Shift +"
If Shift=vbCtrlMask + + vbAltMask vbShiftMask Then StrCode=StrCode & amp; "/Ctrl + Shift + Alt +"
End the If

'hotkey Ctrl + J, breathe out the window
If StrCode="[j] + [Ctrl]" Then
Me. Show
App. TaskVisible=True
End the If
Text1. Text=Text1. Text & amp; Now & amp; "-- -- -- -- -- -" & amp; StrCode & amp; VbCrLf

End Sub
'the key code in String
Private Function CodeToString (nCode As Integer) As String
Dim StrKey As String

The Select Case nCode
Case vbKeyBack: StrKey="BackSpace"
Case vbKeyTab: StrKey="Tab"
Case vbKeyClear: StrKey="Clear"
Case vbKeyReturn: StrKey="Enter"
Case vbKeyShift: StrKey="Shift"
Case vbKeyControl: StrKey="Ctrl"
Case vbKeyMenu: StrKey="Alt"
Case vbKeyPause: StrKey="Pause"
Case vbKeyCapital: StrKey="CapsLock"
Case vbKeyEscape: StrKey="ESC"
Case vbKeySpace: StrKey="SPACEBAR"
Case vbKeyPageUp: StrKey="PAGE UP"
Case vbKeyPageDown: StrKey="PAGE DOWN"
Case vbKeyEnd: StrKey="END"
Case vbKeyHome: StrKey="HOME"
Case vbKeyLeft: StrKey="LEFT ARROW"
Case vbKeyUp: StrKey="UP ARROW"
Case vbKeyRight: StrKey="RIGHT ARROW"
Case vbKeyDown: StrKey="DOWN ARROW"
Case vbKeySelect: StrKey="SELECT"
Case vbKeyPrint: StrKey="PRINT SCREEN"
Case vbKeyExecute: StrKey="EXECUTE"
Case vbKeySnapshot: StrKey="SNAPSHOT"
Case vbKeyInsert: StrKey="INS"
Case vbKeyDelete: StrKey="DEL"
Case vbKeyHelp: StrKey="HELP"
Case vbKeyNumlock: StrKey="NUM LOCK"
Case vbKey0 To vbKey9: StrKey=CRH $(nCode)
Case vbKeyA To vbKeyZ: StrKey=LCase (CRH $(nCode)) 'MapVirtualKeyEx (nCode, 2, GetKeyboardLayout (GetWindowThreadProcessId (GetForegroundWindow, 0))))
Case vbKeyF1 To vbKeyF16: StrKey="F" & amp; CStr (nCode - 111)
Case vbKeyNumpad0 To vbKeyNumpad9: StrKey="Numpad" & amp; CStr (nCode - 96)
Case vbKeyMultiply: StrKey="Numpad {*}"
Case vbKeyAdd: StrKey="Numpad {+}"
Case vbKeySeparator: StrKey="Numpad" {ENTER}
Case vbKeySubtract: StrKey="Numpad {-}"
Case vbKeyDecimal: StrKey="Numpad {...}"
Case vbKeyDivide: StrKey="Numpad/{}"
In Case the Else
StrKey=CRH $(MapVirtualKeyEx (nCode, 2, GetKeyboardLayout (GetWindowThreadProcessId (GetForegroundWindow, 0))))
End the Select
CodeToString="[]" & amp; StrKey & amp; ", "
End the Function
Private Sub text1_Change ()
Text1. SelStart=Len (Text1. Text)
End Sub
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • API
  • Related