Para todos los que estén interesados en iniciarse en los Hooks de bajo nivel (en lo cual yo no soy ningún experto) esto les podría servir, ya que considero haberlo dejado muy bien documentado y se puede aprender algo solo leyendo los comentarios XML.
Simplemente es un Hook que intercepta los eventos del ratón, pudiendo suscribirse a ellos, nada más que eso.
Código
' *********************************************************************** ' Author : Elektro ' Last Modified On : 05-13-2014 ' *********************************************************************** ' <copyright file="MouseHook.vb" company="Elektro Studios"> ' Copyright (c) Elektro Studios. All rights reserved. ' </copyright> ' *********************************************************************** #Region " Instructions " ' Go to page: ' Project > Properties > Debug ' ' And uncheck the option: ' Enable the Visual Studio Hosting Process #End Region #Region " Usage Examples " ' ''' <summary> ' ''' A low level mouse hook that captures mouse events. ' ''' </summary> 'Private WithEvents MouseEvents As MouseHook = Nothing ' ''' <summary> ' ''' Handles the 'MouseLeftDown' event of the Mouse Hook. ' ''' </summary> ' ''' <param name="MouseLocation">Indicates the mouse [X,Y] coordinates.</param> 'Private Sub MouseEvents_MouseLeftDown(ByVal MouseLocation As Point) Handles MouseEvents.MouseLeftDown ' ' Debug.WriteLine(String.Format("Mouse Left Down At: x={0}, y={1}", CStr(MouseLocation.X), CStr(MouseLocation.Y))) ' 'End Sub ' ''' <summary> ' ''' Handles the 'MouseLeftUp' event of the Mouse Hook. ' ''' </summary> ' ''' <param name="MouseLocation">Indicates the mouse [X,Y] coordinates.</param> 'Private Sub MouseEvents_MouseLeftUp(ByVal MouseLocation As Point) Handles MouseEvents.MouseLeftUp ' ' Debug.WriteLine(String.Format("Mouse Left Up At: x={0}, y={1}", CStr(MouseLocation.X), CStr(MouseLocation.Y))) ' 'End Sub ' ''' <summary> ' ''' Handles the 'MouseMove' event of the Mouse Hook. ' ''' </summary> ' ''' <param name="MouseLocation">Indicates the mouse [X,Y] coordinates.</param> 'Private Sub MouseEvents_MouseMove(ByVal MouseLocation As Point) Handles MouseEvents.MouseMove ' ' Debug.WriteLine(String.Format("Mouse Moved To: x={0}, y={1}", CStr(MouseLocation.X), CStr(MouseLocation.Y))) ' 'End Sub ' ''' <summary> ' ''' Handles the 'Click' event of the 'ButtonStartHook' control. ' ''' </summary> 'Private Sub ButtonStartHook() Handles ButtonStartHook.Click ' ' Start the Mouse Hook. ' MouseEvents = New MouseHook 'End Sub ' ''' <summary> ' ''' Handles the 'Click' event of the 'ButtonStopHook' control. ' ''' </summary> 'Private Sub ButtonStopHook() Handles ButtonStopHook.Click ' ' Stop the Mouse Hook. ' MouseEvents = Nothing 'End Sub #End Region #Region " Imports " Imports System.ComponentModel Imports System.Reflection Imports System.Runtime.InteropServices #End Region #Region " MouseHook " ''' <summary> ''' A low level mouse hook class that captures mouse events. ''' </summary> Public Class MouseHook #Region " WinAPI " #Region " Methods " ''' <summary> ''' Passes the hook information to the next hook procedure in the current hook chain. ''' A hook procedure can call this function either before or after processing the hook information. ''' For more info see here: ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644974%28v=vs.85%29.aspx ''' </summary> ''' <param name="idHook"> ''' This parameter is ignored. ''' </param> ''' <param name="nCode"> ''' The hook code passed to the current hook procedure. ''' The next hook procedure uses this code to determine how to process the hook information. ''' </param> ''' <param name="wParam"> ''' The wParam value passed to the current hook procedure. ''' The meaning of this parameter depends on the type of hook associated with the current hook chain. ''' </param> ''' <param name="lParam"> ''' The lParam value passed to the current hook procedure. ''' The meaning of this parameter depends on the type of hook associated with the current hook chain. ''' </param> ''' <returns> ''' This value is returned by the next hook procedure in the chain. ''' The current hook procedure must also return this value. ''' The meaning of the return value depends on the hook type. ''' For more information, see the descriptions of the individual hook procedures. ''' </returns> <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> Private Shared Function CallNextHookEx( ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MSLLHOOKSTRUCT ) As Integer End Function ''' <summary> ''' Installs an application-defined hook procedure into a hook chain. ''' You would install a hook procedure to monitor the system for certain types of events. ''' These events are associated either with a specific thread ''' or with all threads in the same desktop as the calling thread. ''' For more info see here: ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx ''' </summary> ''' <param name="idHook"> ''' The type of hook procedure to be installed. ''' </param> ''' <param name="lpfn"> ''' A pointer to the hook procedure. ''' If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, ''' the lpfn parameter must point to a hook procedure in a DLL. ''' Otherwise, lpfn can point to a hook procedure in the code associated with the current process. ''' </param> ''' <param name="hInstance"> ''' A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. ''' The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by ''' the current process and if the hook procedure is within the code associated with the current process. ''' </param> ''' <param name="threadId"> ''' The identifier of the thread with which the hook procedure is to be associated. ''' For desktop apps, if this parameter is zero, the hook procedure is associated ''' with all existing threads running in the same desktop as the calling thread. ''' </param> ''' <returns> ''' If the function succeeds, the return value is the handle to the hook procedure. ''' If the function fails, the return value is NULL. ''' </returns> <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> Private Shared Function SetWindowsHookEx( ByVal idHook As HookType, ByVal lpfn As MouseProcDelegate, ByVal hInstance As IntPtr, ByVal threadId As Integer ) As Integer End Function ''' <summary> ''' Removes a hook procedure installed in a hook chain by the 'SetWindowsHookEx' function. ''' For more info see here: ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644993%28v=vs.85%29.aspx ''' </summary> ''' <param name="idHook"> ''' A handle to the hook to be removed. ''' This parameter is a hook handle obtained by a previous call to SetWindowsHookEx. ''' </param> ''' <returns> ''' If the function succeeds, the return value is nonzero. ''' If the function fails, the return value is zero. ''' </returns> <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> Private Shared Function UnhookWindowsHookEx( ByVal idHook As Integer ) As Boolean End Function #End Region #Region " Enums " ''' <summary> ''' Indicates a type of Hook procedure to be installed. ''' </summary> <Description("Enum used in 'idHook' parameter of 'SetWindowsHookEx' function")> Private Enum HookType As Integer ' ************************************** ' This enumeration is partially defined. ' ************************************** ''' <summary> ''' Installs a hook procedure that monitors low-level mouse input events. ''' For more information, see the LowLevelMouseProc hook procedure. ''' </summary> WH_MOUSE_LL = 14 End Enum #End Region #Region " Structures " ''' <summary> ''' Contains information about a low-level mouse input event. ''' </summary> <Description("Structure used in 'lParam' parameter of 'CallNextHookEx' function")> Private Structure MSLLHOOKSTRUCT ''' <summary> ''' The ptThe x- and y-coordinates of the cursor, in screen coordinates. ''' </summary> Public pt As Point ''' <summary> ''' If the message is 'WM_MOUSEWHEEL', the high-order word of this member is the wheel delta. ''' The low-order word is reserved. ''' A positive value indicates that the wheel was rotated forward, away from the user; ''' a negative value indicates that the wheel was rotated backward, toward the user. ''' One wheel click is defined as 'WHEEL_DELTA', which is '120'. ''' </summary> Public mouseData As Integer ''' <summary> ''' The event-injected flag. ''' </summary> Public flags As Integer ''' <summary> ''' The time stamp for this message. ''' </summary> Public time As Integer ''' <summary> ''' Additional information associated with the message. ''' </summary> Public dwExtraInfo As Integer End Structure #End Region #End Region #Region " Variables " ''' <summary> ''' ''' </summary> Private MouseHook As Integer #End Region #Region " Delegates " ''' <summary> ''' Delegate MouseProcDelegate ''' </summary> ''' <returns>System.Int32.</returns> Private Delegate Function MouseProcDelegate( ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT ) As Integer ''' <summary> ''' </summary> Private MouseHookDelegate As MouseProcDelegate #End Region #Region " Enums " ''' <summary> ''' Indicates a Windows Message related to a mouse events. ''' For more info see here: ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ff468877%28v=vs.85%29.aspx ''' </summary> Private Enum MouseWindowsMessages As Integer ''' <summary> ''' Posted to a window when the cursor moves. ''' If the mouse is not captured, the message is posted to the window that contains the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_MOUSEMOVE = &H200 ''' <summary> ''' Posted when the user presses the left mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_LBUTTONDOWN = &H201 ''' <summary> ''' Posted when the user releases the left mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_LBUTTONUP = &H202 ''' <summary> ''' Posted when the user double-clicks the left mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_LBUTTONDBLCLK = &H203 ''' <summary> ''' Posted when the user presses the right mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_RBUTTONDOWN = &H204 ''' <summary> ''' Posted when the user releases the right mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_RBUTTONUP = &H205 ''' <summary> ''' Posted when the user double-clicks the right mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_RBUTTONDBLCLK = &H206 ''' <summary> ''' Posted when the user presses the middle mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_MBUTTONDOWN = &H207 ''' <summary> ''' Posted when the user releases the middle mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_MBUTTONUP = &H208 ''' <summary> ''' Posted when the user double-clicks the middle mouse button while the cursor is in the client area of a window. ''' If the mouse is not captured, the message is posted to the window beneath the cursor. ''' Otherwise, the message is posted to the window that has captured the mouse ''' </summary> WM_MBUTTONDBLCLK = &H209 ''' <summary> ''' Sent to the active window when the mouse's horizontal scroll wheel is tilted or rotated. ''' The DefWindowProc function propagates the message to the window's parent. ''' There should be no internal forwarding of the message, ''' since DefWindowProc propagates it up the parent chain until it finds a window that processes it. ''' </summary> WM_MOUSEWHEEL = &H20A End Enum ''' <summary> ''' Indicates the whell direction of the mouse. ''' </summary> Public Enum WheelDirection ''' <summary> ''' The wheel is moved up. ''' </summary> WheelUp ''' <summary> ''' The wheel is moved down. ''' </summary> WheelDown End Enum #End Region #Region " Events " ''' <summary> ''' Occurs when the mouse moves. ''' </summary> Public Event MouseMove(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse left button is pressed. ''' </summary> Public Event MouseLeftDown(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse left button is released. ''' </summary> Public Event MouseLeftUp(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse left button is double-clicked. ''' </summary> Public Event MouseLeftDoubleClick(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse right button is pressed. ''' </summary> Public Event MouseRightDown(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse right button is released. ''' </summary> Public Event MouseRightUp(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse right button is double-clicked. ''' </summary> Public Event MouseRightDoubleClick(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse middle button is pressed. ''' </summary> Public Event MouseMiddleDown(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse middle button is released. ''' </summary> Public Event MouseMiddleUp(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when the mouse middle button is double-clicked. ''' </summary> Public Event MouseMiddleDoubleClick(ByVal MouseLocation As Point) ''' <summary> ''' Occurs when [mouse move]. ''' </summary> Public Event MouseWheel(ByVal MouseLocation As Point, ByVal WheelDirection As WheelDirection) #End Region #Region " Constructors " ''' <summary> ''' Initializes a new instance of this class. ''' </summary> Public Sub New() MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc) MouseHook = SetWindowsHookEx(HookType.WH_MOUSE_LL, MouseHookDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0) End Sub #End Region #Region " Protected Methods " ''' <summary> ''' Allows an object to try to free resources ''' and perform other cleanup operations before it is reclaimed by garbage collection. ''' </summary> Protected Overrides Sub Finalize() UnhookWindowsHookEx(MouseHook) MyBase.Finalize() End Sub #End Region #Region " Private Methods " ''' <summary> ''' Processes the mouse windows messages and raises it's corresponding events. ''' </summary> ''' <returns>System.Int32.</returns> Private Function MouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT ) As Integer If nCode = 0 Then Select Case wParam Case MouseWindowsMessages.WM_MOUSEMOVE RaiseEvent MouseMove(lParam.pt) Case MouseWindowsMessages.WM_LBUTTONDOWN RaiseEvent MouseLeftDown(lParam.pt) Case MouseWindowsMessages.WM_LBUTTONUP RaiseEvent MouseLeftUp(lParam.pt) Case MouseWindowsMessages.WM_LBUTTONDBLCLK RaiseEvent MouseLeftDoubleClick(lParam.pt) Case MouseWindowsMessages.WM_RBUTTONDOWN RaiseEvent MouseRightDown(lParam.pt) Case MouseWindowsMessages.WM_RBUTTONUP RaiseEvent MouseRightUp(lParam.pt) Case MouseWindowsMessages.WM_RBUTTONDBLCLK RaiseEvent MouseRightDoubleClick(lParam.pt) Case MouseWindowsMessages.WM_MBUTTONDOWN RaiseEvent MouseMiddleDown(lParam.pt) Case MouseWindowsMessages.WM_MBUTTONUP RaiseEvent MouseMiddleUp(lParam.pt) Case MouseWindowsMessages.WM_MBUTTONDBLCLK RaiseEvent MouseMiddleDoubleClick(lParam.pt) Case MouseWindowsMessages.WM_MOUSEWHEEL Dim wDirection As WheelDirection If lParam.mouseData < 0 Then wDirection = WheelDirection.WheelDown Else wDirection = WheelDirection.WheelUp End If RaiseEvent MouseWheel(lParam.pt, wDirection) End Select End If Return CallNextHookEx(MouseHook, nCode, wParam, lParam) End Function #End Region End Class #End Region