Imports System.Runtime.InteropServices
Imports System.Reflection
Module keyHook
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Public Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
' Low-Level Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const LLKHF_EXTENDED As Integer = &H1
Private Const MOD_CONTROL As Long = &H2
Private Const LLKHF_INJECTED As Integer = &H10
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const LLKHF_UP As Integer = &H80
Private Const LLKHF_DOWN As Integer = &H81
Private Const VK_F1 As Integer = &H70 'F1 key
Private Const VK_F2 As Integer = &H71 'F2 key
Private Const VK_F3 As Integer = &H72 'F3 key
Private Const VK_F4 As Integer = &H73 'F4 key
Private Const VK_F5 As Integer = &H74 'F5 key
Private Const VK_F6 As Integer = &H75 'F6 key
Private Const VK_F7 As Integer = &H76 'F7 key
Private Const VK_F8 As Integer = &H77 'F8 key
Private Const VK_F9 As Integer = &H78 'F9 key
Private Const VK_F10 As Integer = &H79 'F10 key
Private Const VK_F11 As Integer = &H7A 'F11 key
Private Const VK_F12 As Integer = &H7B 'F12 key
Public Const VK_CONTROL = &H11 'Control
Public Const VK_PRIOR = &H21 'PAGE UP key
Public Const VK_NEXT = &H22 'Page DOWN key
Private Const WH_KEYBOARD_LL As Integer = &H13
Public KeyboardHandle As Integer
Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
If (Hookstruct.vkCode = VK_F1) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) _
And CBool(Hookstruct.flags And LLKHF_UP) Then
MessageBox.Show("Hello World")
Return True
ElseIf (Hookstruct.vkCode = VK_F1) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
Return True
End If
'Etc
End Function
Private Sub HookedState(ByVal Text As String)
End Sub
Public Function KeyboardCallback(ByVal Code As Integer, _
ByVal wParam As Integer, _
ByRef lParam As KBDLLHOOKSTRUCT) As Integer
If (Code = HC_ACTION) Then
Debug.WriteLine("Calling IsHooked")
If (IsHooked(lParam)) Then
Return 1
End If
End If
Return CallNextHookEx(KeyboardHandle, _
Code, wParam, lParam)
End Function
Public Delegate Function KeyboardHookDelegate( _
ByVal Code As Integer, _
ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callback As KeyboardHookDelegate
Public Sub HookKeyboard()
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx( _
WH_KEYBOARD_LL, callback, _
Marshal.GetHINSTANCE( _
[Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
Call CheckHooked()
End Sub
Public Sub CheckHooked()
If (Hooked()) Then
'MsgBox("Keyboard hooked")
Else
MsgBox("Keyboard hook failed: " & Err.LastDllError)
End If
End Sub
Private Function Hooked()
Hooked = KeyboardHandle <> 0
End Function
Public Sub UnhookKeyboard()
If (Hooked()) Then
Call UnhookWindowsHookEx(KeyboardHandle)
End If
End Sub
End Module