Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / XML

Global Windows Hooks

Rate me:
Please Sign up or sign in to vote.
4.80/5 (60 votes)
24 Sep 2010CPOL5 min read 392.1K   11.7K   228  
A single component that contains various Windows hooks
Imports WindowsHookLib
Imports System.Reflection

<Assembly: CLSCompliant(False)> 
Public Class Form1

    'Intptr list object for alowed handles
    Dim mList As New List(Of IntPtr)

#Region " Methods "

    Private Sub CleareDisplay()
        Me.DisplayTextBox.Clear()
        'Print WindowsHooksLib information
        Dim nameAssmb As AssemblyTitleAttribute = Attribute.GetCustomAttribute(MouseHook.AssemblyInfo, GetType(AssemblyTitleAttribute))
        Me.DisplayTextBox.AppendText("Assembly Name: " & nameAssmb.Title)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Dim compAssmb As AssemblyCompanyAttribute = Attribute.GetCustomAttribute(MouseHook.AssemblyInfo, GetType(AssemblyCompanyAttribute))
        Me.DisplayTextBox.AppendText("Company Name: " & compAssmb.Company)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Dim versAssmb As AssemblyVersionAttribute = Attribute.GetCustomAttribute(MouseHook.AssemblyInfo, GetType(AssemblyVersionAttribute))
        Me.DisplayTextBox.AppendText("Assembly Version: " & MouseHook.AssemblyInfo.GetName.Version.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Dim fileVersAssmb As AssemblyFileVersionAttribute = Attribute.GetCustomAttribute(MouseHook.AssemblyInfo, GetType(AssemblyFileVersionAttribute))
        Me.DisplayTextBox.AppendText("Assembly File Version: " & fileVersAssmb.Version)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)

    End Sub

#End Region

    '**************** Low Level Keyboard Hook ****************

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            'Install the keyboard hook
            Me.KeyboardHook1.InstallHook()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            'Remove the mouse hook
            Me.KeyboardHook1.RemoveHook()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub KeyboardHook1_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyboardEventArgs) Handles KeyboardHook1.KeyDown
        'Set the key down Handled property
        e.Handled = Me.HandleKeyboardCheckBox.Checked
        'Print the key down data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== KeyDown")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("KeyCode: " & e.KeyCode.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("KeyValue: " & e.KeyValue)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("KeyData: " & e.KeyData.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Alt: " & e.Alt)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Control: " & e.Control)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Shift: " & e.Shift)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Modifiers: " & e.Modifiers.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End KeyDown")

    End Sub

    Private Sub KeyboardHook1_KeyUp(ByVal sender As Object, ByVal e As WindowsHookLib.KeyboardEventArgs) Handles KeyboardHook1.KeyUp

        'Set the key up Handled property
        e.Handled = Me.HandleKeyboardCheckBox.Checked
        'Print the key up data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== KeyUp")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("KeyCode: " & e.KeyCode.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("KeyValue: " & e.KeyValue)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("KeyData: " & e.KeyData.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Alt: " & e.Alt)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Control: " & e.Control)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Shift: " & e.Shift)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Modifiers: " & e.Modifiers.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End KeyUp")

    End Sub

    Private Sub KeyboardHook1_StateChanged(ByVal sender As Object, ByVal e As WindowsHookLib.StateChangedEventArgs) Handles KeyboardHook1.StateChanged
        'Print the keyboard hook state in the KeyboardGroupBox name 
        Me.KeyboardGroupBox.Text = "Keyboard Hook " & e.State.ToString & "!"
        If e.State = HookState.Uninstalled Then
            'Clear the Handle Keyboard checkbox
            Me.HandleKeyboardCheckBox.Checked = False
        End If
    End Sub

    '*******************************************************
    '*********** End Low Level Keyboard Hook ***************
    '*******************************************************


    '**************** Low Level Mouse Hook *****************

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            'Install the mouse hook
            Me.MouseHook1.InstallHook()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            'Remove the mouse hook
            Me.MouseHook1.RemoveHook()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MouseHook1_MouseDown(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles MouseHook1.MouseDown

        'Set the mouse down Handled property
        e.Handled = Me.HandleMouseCheckBox.Checked And Not Me.mList.Contains(CType(sender, IntPtr))
        'Print the mouse down data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== MouseDown")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End MouseDown")

    End Sub

    Private Sub MouseHook1_MouseUp(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles MouseHook1.MouseUp

        'Set the mouse up Handled property
        e.Handled = Me.HandleMouseCheckBox.Checked And Not Me.mList.Contains(CType(sender, IntPtr))
        'Print the mouse up data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== MouseUp")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End MouseUp")

    End Sub

    Private Sub MouseHook1_MouseClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MouseHook1.MouseClick

        'Print the mouse click data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== MouseClick")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End MouseClick")

    End Sub

    Private Sub MouseHook1_MouseDoubleClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MouseHook1.MouseDoubleClick

        'Print the mouse double click data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== MouseDoubleClick")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End MouseDoubleClick")

    End Sub

    Private Sub MouseHook1_MouseWheel(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles MouseHook1.MouseWheel

        'Set the mouse wheel Handled property
        e.Handled = Me.HandleMouseCheckBox.Checked
        'Print the mouse wheel data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== MouseWheel")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End MouseWheel")

    End Sub

    Private Sub MouseHook1_MouseMove(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles MouseHook1.MouseMove

        'Print the mouse location in the MouseLocationTextBox
        Me.MouseLocationTextBox.Text = e.Location.ToString
        ''Print the mouse move data
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("===================== MouseMove")
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
        'Me.DisplayTextBox.AppendText(Environment.NewLine)
        'Me.DisplayTextBox.AppendText("===================== End MouseMove")

    End Sub

    Private Sub MouseHook1_StateChanged(ByVal sender As Object, ByVal e As WindowsHookLib.StateChangedEventArgs) Handles MouseHook1.StateChanged
        'Print the mouse hook state in the MouseGroupBox name 
        Me.MouseGroupBox.Text = "Mouse Hook " & e.State.ToString & "!"
        If e.State = HookState.Uninstalled Then
            'Clear the mouse location and the Handle Mouse checkbox
            Me.MouseLocationTextBox.Text = String.Empty
            Me.HandleMouseCheckBox.Checked = False
        End If
    End Sub

    '*******************************************************
    '*************** End Low Level Mouse Hook **************
    '*******************************************************


    '**************** Synthesize Mouse Event *****************

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.Modifiers And Keys.Shift) = Keys.Shift And e.KeyCode = Keys.C Then
            Dim p As Point = Me.GroupBox1.PointToScreen(Me.ClickCheckBox.Location)
            p.X += 6
            p.Y += 10
            MouseHook.SynthesizeMouseMove(p, MapOn.PrimaryMonitor, IntPtr.Zero)
            Me.ClickCheckBox.Focus()
            MouseHook.SynthesizeMouseDown(Windows.Forms.MouseButtons.Left, IntPtr.Zero)
            MouseHook.SynthesizeMouseUp(Windows.Forms.MouseButtons.Left, IntPtr.Zero)
        End If
    End Sub

    '*******************************************************
    '************** End Synthesize Mouse Event *************
    '*******************************************************


    '**************** Window Clipboard Hook ****************

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            'Install the clipboard hook
            Me.ClipboardHook1.InstallHook(Me)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Try
            'Remove the clipboard hook
            Me.ClipboardHook1.RemoveHook()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub ClipboardHook1_ClipboardChanged(ByVal sender As Object, ByVal e As WindowsHookLib.ClipboardEventArgs) Handles ClipboardHook1.ClipboardChanged
        'Print the mouse wheel data
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== ClipboardChanged")
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Hooked Window: " & Me.ClipboardHook1.HookedWindow.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Source Window : " & e.SourceWindow.ToString)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Contains Audio: " & My.Computer.Clipboard.ContainsAudio)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Contains Image: " & My.Computer.Clipboard.ContainsImage)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Contains Text: " & My.Computer.Clipboard.ContainsText)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("Contains FileDropList: " & My.Computer.Clipboard.ContainsFileDropList)
        Me.DisplayTextBox.AppendText(System.Environment.NewLine)
        Me.DisplayTextBox.AppendText("===================== End ClipboardChanged")
    End Sub


    Private Sub ClipboardHook1_StateChanged(ByVal sender As Object, ByVal e As WindowsHookLib.StateChangedEventArgs) Handles ClipboardHook1.StateChanged
        'Print the mouse hook state in the MouseGroupBox name 
        Me.Clipboard_GroupBox.Text = "Clipboard Hook " & e.State.ToString & "!"
    End Sub

    '*******************************************************
    '************** End Window Clipboard Hook *************
    '*******************************************************


    '******************* Form's Events *********************

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = "Windows Hook Demo - www.code2point.com"
        Me.CleareDisplay()
        Me.MouseGroupBox.Text = "Mouse Hook " & Me.MouseHook1.State.ToString & "!"
        Me.KeyboardGroupBox.Text = "Keyboard Hook " & Me.KeyboardHook1.State.ToString & "!"
        Me.Clipboard_GroupBox.Text = "Clipboard Hook " & Me.ClipboardHook1.State.ToString & "!"
        'Set the alowed controls list
        For Each ctr As Control In Me.MouseGroupBox.Controls
            Me.mList.Add(ctr.Handle)
        Next
        Me.Label2.Text = "My handle is: " & Me.ClickCheckBox.Handle.ToString
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.KeyboardHook1.Dispose()
        Me.MouseHook1.Dispose()
        Me.ClipboardHook1.Dispose()
    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        Me.CleareDisplay()
    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ZipEdTech
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions