Hi @Mansour_Dalir ,
You can use the SetWindowsHookEx
function from the Windows API to capture global mouse events. Here's an example that demonstrates how to capture the global mouse down event and trigger an action when the left mouse button is clicked:
Imports System.Runtime.InteropServices
Public Class Form1
' Define constants for hook types
Private Const WH_MOUSE_LL As Integer = 14
Private Const WM_LBUTTONDOWN As Integer = &H201
' Declare the hook handle
Private hookID As IntPtr = IntPtr.Zero
' Declare the hook callback delegate
Private mouseProc As LowLevelMouseProc = Nothing
' Declare the structure to store mouse information
<StructLayout(LayoutKind.Sequential)>
Private Structure MSLLHOOKSTRUCT
Public pt As POINT
Public mouseData As UInteger
Public flags As UInteger
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure POINT
Public x As Integer
Public y As Integer
End Structure
' Declare the delegate for the low-level mouse procedure
Private Delegate Function LowLevelMouseProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
' Import the necessary Windows API functions
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetWindowsHookEx(idHook As Integer, lpfn As LowLevelMouseProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function UnhookWindowsHookEx(hhk As IntPtr) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
End Function
' Set up the mouse hook
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mouseProc = AddressOf MouseHookCallback
hookID = SetHook(mouseProc)
End Sub
' Release the hook when the form closes
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
UnhookWindowsHookEx(hookID)
End Sub
' Hook function to set the mouse hook
Private Function SetHook(proc As LowLevelMouseProc) As IntPtr
Using curProcess = Process.GetCurrentProcess()
Using curModule = curProcess.MainModule
Return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
End Using
End Using
End Function
' The callback function that handles the mouse events
Private Function MouseHookCallback(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
If nCode >= 0 AndAlso wParam = CType(WM_LBUTTONDOWN, IntPtr) Then
' The left mouse button was clicked - trigger the action
TriggerAction()
End If
' Call the next hook in the chain
Return CallNextHookEx(hookID, nCode, wParam, lParam)
End Function
' Define the action to trigger on mouse click
Private Sub TriggerAction()
MessageBox.Show("Left mouse button clicked!")
End Sub
End Class
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.