Skip to content

Low level mouse hook

Visual Vincent edited this page Dec 8, 2022 · 10 revisions

Contents:

Creating a low-level mouse hook

To create a low-level mouse hook with InputHelper you simply have to instantiate an instance of the InputHelper.Hooks.KeyboardHook class.

Dim MouseHook As New InputHelper.Hooks.MouseHook()

The hook is now created and will be listening for mouse events.


Removing the hook

The mouse hook can be removed manually by calling its Dispose() method.

'Removing the hook manually.
MouseHook.Dispose()

However, the hook will also be removed automatically if the variable goes out of scope:

'Example of a hook getting removed automatically.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles StopButton.Click
    Dim MouseHook As New InputHelper.Hooks.MouseHook()

    'Stuff happens here...

End Sub 'Once code execution reaches this point the hook variable goes out of scope and will be removed automatically.

Recommended structure

It is recommended that you keep the hook variable at class-level, and dispose it when you no longer need it. This is because it gives you better control over the hook and decreases the risk of it accidentally going out of scope.

'Your form.
Public Class Form1

    'Class-level variable.
    Dim MouseHook As InputHelper.Hooks.MouseHook

    'Instantiate the hook on form load (if you want to).
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        MouseHook = New InputHelper.Hooks.MouseHook()
    End Sub

    'Dispose of the hook when the form has closed.
    Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        MouseHook.Dispose()
    End Sub

    'The rest of your code...

End Class

Below is an example of using the hook in a form with a Start and Stop button, where the hook is started automatically when the form opens:

Public Class Form1
    Dim MouseHook As InputHelper.Hooks.MouseHook

    Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If MouseHook IsNot Nothing Then MouseHook.Dispose() 'Dispose the hook if it is still active when the form has closed.
    End Sub

    Private Sub StartButton_Click(sender As System.Object, e As System.EventArgs) Handles StartButton.Click
        If MouseHook IsNot Nothing Then MouseHook.Dispose() 'Remove the old hook before creating a new one.
        MouseHook = New InputHelper.Hooks.MouseHook()
    End Sub

    Private Sub StopButton_Click(sender As System.Object, e As System.EventArgs) Handles StopButton.Click
         If MouseHook IsNot Nothing Then MouseHook.Dispose()
    End Sub
End Class

Responding to events

The mouse hook includes four events. They are responsible for notifying the programmer when a mouse event occurs.

  • MouseDown - Occurs when a mouse button is pressed or held down.
  • MouseMove - Occurs when the mouse moves.
  • MouseUp - Occurs when a mouse button is released.
  • MouseWheel - Occurs when the mouse wheel is scrolled.

Adding event handlers

Adding an event handler to one of the MouseHook's events can be done in two different ways: Either with the AddHandler statement or using a combination of the WithEvents keyword and Handles clause.

Method 1: Using AddHandler

Dim MouseHook As InputHelper.Hooks.MouseHook

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    MouseHook = New InputHelper.Hooks.MouseHook()

    'Adding event handlers to the MouseHook events.
    AddHandler MouseHook.MouseDown, AddressOf MouseHook_MouseDown
    AddHandler MouseHook.MouseMove, AddressOf MouseHook_MouseMove
    AddHandler MouseHook.MouseUp, AddressOf MouseHook_MouseUp
    AddHandler MouseHook.MouseWheel, AddressOf MouseHook_MouseWheel
End Sub

Private Sub MouseHook_MouseDown(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    'MouseDown event.
End Sub

Private Sub MouseHook_MouseMove(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    'MouseMove event.
End Sub

Private Sub MouseHook_MouseUp(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    'MouseUp event.
End Sub

Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    'MouseWheel event.
End Sub

Method 2: Using WithEvents and the Handles clause

'When using WithEvents and the Handles clause the hook must be initialized immediately at class-level.
Dim WithEvents MouseHook As New InputHelper.Hooks.MouseHook()

Private Sub MouseHook_MouseDown(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs) Handles MouseHook.MouseDown
    'MouseDown event.
End Sub

Private Sub MouseHook_MouseMove(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs) Handles MouseHook.MouseMove
    'MouseMove event.
End Sub

Private Sub MouseHook_MouseUp(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs) Handles MouseHook.MouseUp
    'MouseUp event.
End Sub

Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs) Handles MouseHook.MouseWheel
    'MouseWheel event.
End Sub

NOTE: It is recommended to use the AddHandler solution if the hook may be disposed/reinstantiated multiple times during the lifetime of the form.

In Method 1 you need to use AddHandler every time you reinstantiate the hook.


Event Args

Event Arguments (or Event Args for short) are a set of properties passed along with an event that contains information and data from, or about, the event.

InputHelper's event arguments can be accessed through the e parameter of the event.

Private Sub MouseHook_MouseDown(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    MessageBox.Show("Mouse button clicked: " & e.Button.ToString() & ", Double-click: " & e.DoubleClick.ToString())
End Sub

Private Sub MouseHook_MouseMove(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    TextBox1.AppendText("Mouse moved to (X: " & e.Location.X & ", Y: " & e.Location.Y & ")")
End Sub

Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelper.EventArgs.MouseHookEventArgs)
    MessageBox.Show("Mouse scroll: " & e.ScrollDirection.ToString() & ", Amount: " & e.Delta)
End Sub

Available properties are:

Property Access Description
Block Read, Write Whether the mouse input should be blocked from reaching any windows.
CAUTION: Blocked mouse input will not be handled by any application, nor Windows itself! Any problems caused by using this property is your own responsibility! (Any event)
Button Read The mouse button that was pressed or released (MouseDown and MouseUp events only).
ButtonState Read The state of the button that generated the event (Down or Up) (MouseDown and MouseUp events only).
Delta Read The amount that the mouse wheel has scrolled. This is positive if the mouse wheel is rotated upwards (away from the user) or negative if the mouse wheel is rotated in a downwards (toward the user) (MouseWheel event only).
DoubleClick Read Whether the clicked button was caused by a double-click (MouseDown event only).
Injected Read Whether the mouse event was injected into the input stream by a process rather than an input device.
InjectedAtLowerIL Read Whether the mouse event was injected into the input stream by another process running at lower integrity level.
Location Read The current location of the mouse (in screen coordinates) (Any event).
ScrollDirection Read In which direction the mouse wheel was scrolled (vertically or horizontally) (MouseWheel event only).