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 |
Declare Function UnHookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long |
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long |
Public hnextHookproc As Long Public Const WH_KEYBOARD = 2 ‘这个是表明Hook的种类是键盘Hook Public Const PM_KEY_SPACE = &H20 ‘空格键 |
Public Sub UnHookKBD() ‘解键盘HOOK函数 If hnextHookproc <> 0 Then UnHookWindowsHookEx hnextHookproc hnextHookproc = 0 End If End Sub Public Function EnableKBDHook() ‘设置键盘HOOK If hnextHookproc <> 0 Then Exit Function End If hnextHookproc = SetWindowsHookEx(WH_KEYBOARD, AddressOf MyKBHFunc, App.hInstance, 0) If hnextHookproc <> 0 Then EnableKBDHook = hnextHookproc End If End Function Public Function MyKBHFunc(ByVal iCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long MyKBHFunc = 0 If iCode < 0 Then MyKBHFunc = CallNextHookEx(hnextHookproc, iCode, wParam, lParam) Exit Function End If If wParam = PM_KEY_SPACE Then ‘‘偵測 有沒有按到空格鍵 MyKBHFunc = 1 ‘加入自己的代码,用于表明响应 form1.text1.text=”Hook成功!” End If End Function |
Private Sub Form_Load() Call EnableKBDHook End Sub Private Sub Form_Unload(Cancel As Integer) Call UnHookKBD End Sub |
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As HookType, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer |
hnextHookproc = SetWindowsHookEx(WH_KEYBOARD, AddressOf MyKBHFunc, App.hInstance, 0) |
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer |
Public hnextHookproc As Long Public Const WH_KEYBOARD = 2 ‘这个是表明Hook的种类是键盘Hook Public Const PM_KEY_SPACE = &H20 ‘空格键 |
Public Enum HookType WH_KEYBOARD = 2 End Enum |
Module Module1 Public frm1 As New Form1() ‘这个的作用,最后再说 Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer Declare Function SetWindowsHookEx Lib "user32" Alias _ "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, _ ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer Declare Function UnhookWindowsHookEx Lib "user32" _ (ByVal hHook As Integer) As Integer Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, _ ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer Public hnexthookproc As Integer Public Const PM_KEY_SPACE = &H20 Public Enum HookType WH_KEYBOARD = 2 End Enum Public Sub UnHook() ‘解Hook If hnexthookproc <> 0 Then UnhookWindowsHookEx(hnexthookproc) hnexthookproc = 0 End If End Sub Public Function SetHook() ‘设置Hook If hnexthookproc <> 0 Then Exit Function End If hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD, AddressOf MyKeyboardProc, 0, GetCurrentThreadId()) |
End Function Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer MyKeyboardProc = 0 If nCode < 0 Then MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam) Exit Function End If If wParam = PM_KEY_SPACE Then MyKeyboardProc = 1 ‘写入你自己的代码 frm1.textbox1.text=”HOOK成功!” End If End Function Sub main() Application.Run(frm1) End Sub End Module |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Call SetHook() End Sub ‘vb.net中没有form_unload事件了,而是用closing Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Call UnHook() End Sub |
联系客服