Link to home
Start Free TrialLog in
Avatar of alexander001
alexander001

asked on

Multiple keyboard hooks

I need to make multiple keyboard hooks.  One for PrtScrn, F8 and F9 keys.

I have the keyboardhook class, and the sub that handles the key.  

I can get the Prtscrn one working.  however i am not sure how to code the other two so the objects/classes do not conflict.  

Private WithEvents llkb As LowLevelKeyBoardhook
 
 
Private Sub llkb_PrtScr() Handles llkb.PrtScr
   timer.enabled = true
End Sub
 
 
Private Class LowLevelKeyBoardhook
 
        Private Const HC_ACTION As Integer = 0
        Private Const WH_KEYBOARD_LL As Integer = 13
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_SYSKEYDOWN As Integer = &H104
 
        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
 
        Private Declare Function SetWindowsHookEx Lib "user32" _
            Alias "SetWindowsHookExA" ( _
            ByVal idHook As Integer, _
            ByVal lpfn As LowLevelKeyboardProcDelegate, _
            ByVal hmod As Integer, _
            ByVal dwThreadId 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
 
        Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
            ByVal hHook As Integer) As Integer
 
        Private Delegate Function LowLevelKeyboardProcDelegate( _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByRef lParam As KBDLLHOOKSTRUCT) As Integer
 
        Public Event PrtScr()
 
        Private hhkLowLevelKeyboard As Integer
        Private keyboardDelegate As LowLevelKeyboardProcDelegate
 
        Public Sub New()
            keyboardDelegate = New LowLevelKeyboardProcDelegate(AddressOf Me.LowLevelKeyboardProc)
            hhkLowLevelKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardDelegate, _
                Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub
 
        Private Function LowLevelKeyboardProc( _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByRef lParam As KBDLLHOOKSTRUCT) As Integer
 
            If (nCode = HC_ACTION) Then
                Select Case wParam
                    Case WM_KEYDOWN, WM_SYSKEYDOWN
                        If lParam.vkCode = Keys.PrintScreen Then
                            RaiseEvent PrtScr()
                        End If
                End Select
            End If
 
            Return CallNextHookEx(hhkLowLevelKeyboard, nCode, wParam, lParam)
        End Function
 
        Protected Overrides Sub Finalize()
            UnhookWindowsHookEx(hhkLowLevelKeyboard)
            MyBase.Finalize()
        End Sub
 
    End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial