How to create the MouseDown event on the desktop screen

Mansour_Dalir 1,856 Reputation points
2024-09-03T05:56:47.2033333+00:00

How can I create a MouseDown event on the desktop screen using Visual Basic? I want to trigger a specific action when the left mouse button is clicked. Any suggestions or examples would be appreciated.

I got such a response, but it is not possible to make changes in the form from the event received.Answer 'Q&A Assist'

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,575 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,712 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 30,931 Reputation points Microsoft Vendor
    2024-09-03T08:13:34.9666667+00:00

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2024-09-03T05:56:47.22+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To create a MouseDown event on the desktop screen using Visual Basic, you can use a low-level mouse hook to detect mouse events. Here is an example code snippet that demonstrates how to do this:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const WH_MOUSE_LL As Integer = 14
        Private Const WM_LBUTTONDOWN As Integer = &H201
    
        Private Delegate Function LowLevelMouseProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    
        Private Shared Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
            If nCode >= 0 AndAlso wParam = New IntPtr(WM_LBUTTONDOWN) Then
                ' Left mouse button was clicked
                ' Add your code here to perform the desired action
            End If
            Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
        End Function
    
        Private Shared _hookID As IntPtr = IntPtr.Zero
        Private Shared _proc As LowLevelMouseProc = AddressOf HookCallback
    
        Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelMouseProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
        Private Declare Function CallNextHookEx Lib "user32" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As IntPtr) As Boolean
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            _hookID = SetWindowsHookEx(WH_MOUSE_LL, _proc, IntPtr.Zero, 0)
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            UnhookWindowsHookEx(_hookID)
        End Sub
    End Class
    

    This code sets up a low-level mouse hook that listens for left mouse button clicks. When a left mouse button click is detected, you can add your own code to perform the desired action. Note that this code will detect left mouse button clicks anywhere on the desktop screen, not just within your application.

    References:

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.