Keyboard hooks

leighcurran

New member
Joined
Jul 22, 2008
Messages
2
Programming Experience
Beginner
Hi All,
Below is some of the code from my first .NET app so please go easy .

I have hooked the keyboard using KBHook.dll from:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2971&lngWId=10 But what I am having problems with is calling the Sub m_Hook_KeyDown from within Sub Timer1_Tick. Basically it hooks the keyboard at all times instead of when the network is disconnected.

Thanks heaps for you help!!!!



VB.NET:
Imports KBHook

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connection.Tick
        Dim Connection As Boolean
        Connection = My.Computer.Network.IsAvailable

        If Connection = False Then
            Dim n
            For n = 1 To 10
                Me.Opacity() = Me.Opacity() + 0.1
                Me.WindowState = FormWindowState.Maximized
            Next
            If Not CountDown.Enabled = True Then
                CountDown.Start()
            End If
            If Not KillTaskManager.Enabled = True Then
                KillTaskManager.Start()
            End If

            'THIS IS WHERE I WOULD LIKE TO HOOK THE KEYBOARD

        Else
            Dim n
            For n = 1 To 10
                Me.Opacity() = Me.Opacity() - 0.1
                Me.WindowState = FormWindowState.Minimized
            Next
            ProgressBar.Value = 0

            CountDown.Stop()
            KillTaskManager.Stop()

            'THIS IS WHERE I WOULD LIKE TO UN-HOOK THE KEYBOARD

        End If
    End Sub
 

'MOVE THIS SUB???   
Private WithEvents m_Hook As New KeyboardLowLevelHook
    
Private Sub m_Hook_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles m_Hook.KeyDown
        'Block key combinations
        If (e.KeyCode = Keys.F4 And e.Alt) Or (e.KeyCode = Keys.Tab And e.Alt) Then
            e.Handled = True
        End If
    End Sub
 
Last edited by a moderator:
As the class is set up, assign a new KeyboardLowLevelHook instance to your hook variable when you want it active, Dispose it when you want to remove. In the short code you may notice the constructor calls SetWindowsHookEx while Dispose method calls UnhookWindowsHookEx.
 
Back
Top