Vb Net - Low Level Mouse Hook (Global)

Installing a Low Level Mouse Hook

MouseHookYeah!

This hook is basically the same as the Keyboard hook, just, with a mouse and a few extra things…

Below is the basic class that you need:

Private Class MouseHook

”Constants

Private Const HC_ACTION As Integer = 0

Private Const WH_MOUSE_LL As Integer = 14

Private Const WM_MOUSEMOVE As Integer = &H200

Private Const WM_LBUTTONDOWN As Integer = &H201

Private Const WM_LBUTTONUP As Integer = &H202

Private Const WM_LBUTTONDBLCLK As Integer = &H203

Private Const WM_RBUTTONDOWN As Integer = &H204

Private Const WM_RBUTTONUP As Integer = &H205

Private Const WM_RBUTTONDBLCLK As Integer = &H206

Private Const WM_MBUTTONDOWN As Integer = &H207

Private Const WM_MBUTTONUP As Integer = &H208

Private Const WM_MBUTTONDBLCLK As Integer = &H209

Private Const WM_MOUSEWHEEL As Integer = &H20A

”Mouse Structures

Public Structure POINT

Private x As Integer

Private y As Integer

End Structure

Private Structure MSLLHOOKSTRUCT

Private pt As POINT

Private mouseData As Integer

Private flags As Integer

Private time As Integer

Private dwExtraInfo As Integer

End Structure

”API Functions

Private Declare Function SetWindowsHookEx Lib “user32″ Alias “SetWindowsHookExA” (ByVal idHook As Integer, ByVal lpfn As MouseProcDelegate, 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 MSLLHOOKSTRUCT) As Integer

Private Declare Function UnhookWindowsHookEx Lib “user32″ (ByVal hHook As Integer) As Integer

”Our Mouse Delegate

Private Delegate Function MouseProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer

”The Mouse events

Public Shared Event MouseMove()

Public Shared Event MouseEvent(ByVal mEvent As Integer)

”The identifyer for our MouseHook

Private Shared MouseHook As Integer

”MouseHookDelegate

Private Shared MouseHookDelegate As MouseProcDelegate

Public Sub New()

”Installs a Low Level Mouse Hook

MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc)

MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookDelegate, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)

End Sub

Private Shared Function MouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer

”If it is a Mouse event

If (nCode = HC_ACTION) Then

If wParam = WM_MOUSEMOVE Then

”If it is the mouse moving

RaiseEvent MouseMove()

ElseIf wParam = WM_LBUTTONDOWN Or wParam = WM_LBUTTONUP Or wParam = WM_LBUTTONDBLCLK Or wParam = WM_RBUTTONDOWN Or wParam = WM_RBUTTONUP Or wParam = WM_RBUTTONDBLCLK Or wParam = WM_MBUTTONDOWN Or wParam = WM_MBUTTONUP Or wParam = WM_MBUTTONDBLCLK Or wParam = WM_MOUSEWHEEL Then

”If it is a different mouse event

RaiseEvent MouseEvent(wParam)

End If

End If

”Next

Return CallNextHookEx(MouseHook, nCode, wParam, lParam)

End Function

Protected Overrides Sub Finalize()

”On close it UnHooks the Hook

UnhookWindowsHookEx(MouseHook)

MyBase.Finalize()

End Sub

End Class

The important part this time is in the MouseProc function

If (nCode = HC_ACTION) Then

If wParam = WM_MOUSEMOVE Then

”If it is the mouse moving

RaiseEvent MouseMove()

ElseIf wParam = WM_LBUTTONDOWN Or wParam = WM_LBUTTONUP Or wParam = WM_LBUTTONDBLCLK Or wParam = WM_RBUTTONDOWN Or wParam = WM_RBUTTONUP Or wParam = WM_RBUTTONDBLCLK Or wParam = WM_MBUTTONDOWN Or wParam = WM_MBUTTONUP Or wParam = WM_MBUTTONDBLCLK Or wParam = WM_MOUSEWHEEL Then

”If it is a different mouse event

RaiseEvent MouseEvent(wParam)

End If

End If

This raises one of the two events: MouseMove or MouseEvent

Private Shadows Sub MouseMove() Handles MHook.MouseMove

”This keeps the form on top, and sets it so that it moves with the cursor

Me.BringToFront()

Me.Location = New Point(Windows.Forms.Cursor.Position.X - (Me.Width / 2), Windows.Forms.Cursor.Position.Y - (Me.Height / 2))

End Sub

That’s just a basic sub that moves the form’s centre to the location of the mouse cursor, whenever the mouse is moved it follows it around.

Private Sub MouseEvent(ByVal mEvent As Integer) Handles MHook.MouseEvent

”Saves a basic log of the mouse events

Dim x As Integer = Windows.Forms.Cursor.Position.X

Dim y As Integer = Windows.Forms.Cursor.Position.Y

If mEvent = 513 Then

Log &= Now & ” - Left Down - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = 514 Then

Log &= Now & ” - Left Up - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H203 Then

Log &= Now & ” - Left Double Click - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H204 Then

Log &= Now & ” - Right Down - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H205 Then

Log &= Now & ” - Right Up - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H206 Then

Log &= Now & ” - Right Double Click - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H207 Then

Log &= Now & ” - Middle Down - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H208 Then

Log &= Now & ” - Middle Up - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H209 Then

Log &= Now & ” - Middle Double Click - (” & x & “, “ & y & “)” & vbNewLine

ElseIf mEvent = &H20A Then

Log &= Now & ” - Mouse Scroll - (” & x & “, “ & y & “)” & vbNewLine

”Displays the log

MsgBox(Log)

End If

End Sub

That has a little more to it, it determines what the event was, then saves a log accordingly.

Now, in order to make this function, just add the MouseHook class, and the two subs to your form along with this:

Private WithEvents MHook As MouseHook

Dim Log As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

”Hooks The Mouse

MHook = New MouseHook

End Sub

Which makes the whole thing work J

Oh, and just for the record, either run this program OUTSIDE the IDE or:

In solution explorer, click on My Project

Go to Debug

Uncheck ‘Enable the Visual Studio hosting process’

Same goes with the keyboard hook.

1 Comment so far

  1. Hani Salem on May 9, 2008

    Hi
    you have a nice work here friend
    Thank you to manage the site on topics

Leave a reply