Pouknouki

VB.NET Mouse Hook

Feb 26th, 2013
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class MouseHook
  2.     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
  3.     Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer
  4.     Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
  5.     Private Delegate Function MouseProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer
  6.  
  7.     Private Structure MSLLHOOKSTRUCT
  8.         Public pt As Point
  9.         Public mouseData As Integer
  10.         Public flags As Integer
  11.         Public time As Integer
  12.         Public dwExtraInfo As Integer
  13.     End Structure
  14.     Public Enum Wheel_Direction
  15.         WheelUp
  16.         WheelDown
  17.     End Enum
  18.  
  19.     Private Const HC_ACTION As Integer = 0
  20.     Private Const WH_MOUSE_LL As Integer = 14
  21.     Private Const WM_LBUTTONDOWN As Integer = &H201
  22.     Private Const WM_RBUTTONDOWN As Integer = &H204
  23.     Private Const WM_MBUTTONDOWN As Integer = &H207
  24.     Private Const WM_MOUSEWHEEL As Integer = &H20A
  25.     Private Const WM_XBUTTONDOWN As Integer = &H20B
  26.     Private MouseHook As Integer
  27.     Private MouseHookDelegate As MouseProcDelegate
  28.     Public Event Mouse_Left()
  29.     Public Event Mouse_Right()
  30.     Public Event Mouse_Middle()
  31.     Public Event Mouse_Wheel(ByVal Direction As Wheel_Direction)
  32.     Public Event Mouse_XButton1()
  33.     Public Event Mouse_XButton2()
  34.  
  35.     Public Sub New()
  36.         MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc)
  37.         MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
  38.     End Sub
  39.  
  40.     Private Function MouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer
  41.         If (nCode = HC_ACTION) Then
  42.             Select Case wParam
  43.                 Case WM_LBUTTONDOWN
  44.                     RaiseEvent Mouse_Left()
  45.                 Case WM_RBUTTONDOWN
  46.                     RaiseEvent Mouse_Right()
  47.                 Case WM_MBUTTONDOWN
  48.                     RaiseEvent Mouse_Middle()
  49.                 Case WM_MOUSEWHEEL
  50.                     Dim wDirection As Wheel_Direction
  51.                     If lParam.mouseData < 0 Then
  52.                         wDirection = Wheel_Direction.WheelDown
  53.                     Else
  54.                         wDirection = Wheel_Direction.WheelUp
  55.                     End If
  56.                     RaiseEvent Mouse_Wheel(wDirection)
  57.                 Case (WM_MOUSEWHEEL)
  58.                     Dim wDirection As Wheel_Direction
  59.                     If lParam.mouseData < 0 Then
  60.                         wDirection = Wheel_Direction.WheelDown
  61.                     Else
  62.                         wDirection = Wheel_Direction.WheelUp
  63.                     End If
  64.                     RaiseEvent Mouse_Wheel(wDirection)
  65.             End Select
  66.         End If
  67.         Return CallNextHookEx(MouseHook, nCode, wParam, lParam)
  68.     End Function
  69.  
  70.     Protected Overrides Sub Finalize()
  71.         UnhookWindowsHookEx(MouseHook)
  72.         MyBase.Finalize()
  73.     End Sub
  74. End Class
RAW Paste Data