none
Simulate a mouse click in a program

    Question

  • I have a VB program and I need to simulate a mouse click in a webbrowser window. How would I go about simulating that click on the screen.

    Thanks
    Saturday, November 19, 2005 1:47 PM

Answers

  • Hi There,
    This is done using API call, This is the way to do any mouse action you need:

    1- First you declare some API Functions, Subs and Constants

    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

    2- Then you call your functions and subs
    2.1- To move the cusor:
       SetCursorPos(X,Y) ' Where X and Y are in pixel
    2.2- To get cursor location
       Dim tempPos As Point
       
    Dim R As Long = GetCursorPos(tempPos) ' You'll get your location in TempPos
    2.3- To semulate clicks
    2.3.1 Left click
       mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
       mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    2.3.2 Right click
       mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
       mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
    2.3.3 Middle Click
       mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
       mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

    2.4- To semulate drag and drop:
       mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
       SetCursorPos(X,Y) ' Where X and Y are in pixel
       mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)


    I hope that answered your problem :)

    If you have any technical questions about APIs or any other detail in my message feel free to contact me.

    Abdullah Al-Rasheed

    Saturday, November 19, 2005 5:24 PM
  • Most often you'd use the API FindWindow().  The VS tool Microsoft Spy++ comes in mighty handy for determining the parameter values.

    This is, however, not one of those FindWindow() cases.  If you Spy++ the button, you will find that the window is the "Internet Explorer_Server" (class) window.  That is, if you are using IE.

    In this case, a way to determine the coordinates of the button is to use DHTML.  Best yet, use the most browser-agnostic DHTML possible.  I recommend using getElementById() to get the button.

    Once you have the button, use the properties to determine where the button is coordinate-wise.  The following documentation should be helpful:
    http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/input_submit.asp

    Then tie this in with Abdullah's code.
    Tuesday, November 22, 2005 9:12 PM

All replies

  • Hi There,
    This is done using API call, This is the way to do any mouse action you need:

    1- First you declare some API Functions, Subs and Constants

    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

    2- Then you call your functions and subs
    2.1- To move the cusor:
       SetCursorPos(X,Y) ' Where X and Y are in pixel
    2.2- To get cursor location
       Dim tempPos As Point
       
    Dim R As Long = GetCursorPos(tempPos) ' You'll get your location in TempPos
    2.3- To semulate clicks
    2.3.1 Left click
       mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
       mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    2.3.2 Right click
       mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
       mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
    2.3.3 Middle Click
       mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
       mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

    2.4- To semulate drag and drop:
       mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
       SetCursorPos(X,Y) ' Where X and Y are in pixel
       mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)


    I hope that answered your problem :)

    If you have any technical questions about APIs or any other detail in my message feel free to contact me.

    Abdullah Al-Rasheed

    Saturday, November 19, 2005 5:24 PM
  • Abdullah,

    Is this is VB6 code, you can tell by the long declarations in the api calls.

    Since this is a .net forum, it's very safe to assume that users are seeking .dot net solutions.

    Your solution may or may not work were it moved in dot net if the longs were changed to integers. The reason that no one could say right off the bat is because it actually depends on how the dll passes data back to the caller. Very often marshalling and interop interfaces are required.
    Saturday, November 19, 2005 6:35 PM
  • how would one get the position of a button in the webbrowser control, assuming one knows the name of the button? or would one have to take a guess
    Saturday, November 19, 2005 8:53 PM
  • Hi ReneeC,

       This was code is not VB6!! although I got all API declares from 'API Browser' but I modified the code to work with VB.NET and by the way I never send any code before testing it.

       And about the Long decalred parameters, they are not used in the program. so it doesn't realy matter if you use int32 'Integer' or int64 'Long'.

      Anyway it's better to change Long paramters to Integer when you get API declares from 'API Browser' even if you'r not going to use them :)

    Abdullah Al-Rasheed
    Sunday, November 20, 2005 1:00 PM
  • Most often you'd use the API FindWindow().  The VS tool Microsoft Spy++ comes in mighty handy for determining the parameter values.

    This is, however, not one of those FindWindow() cases.  If you Spy++ the button, you will find that the window is the "Internet Explorer_Server" (class) window.  That is, if you are using IE.

    In this case, a way to determine the coordinates of the button is to use DHTML.  Best yet, use the most browser-agnostic DHTML possible.  I recommend using getElementById() to get the button.

    Once you have the button, use the properties to determine where the button is coordinate-wise.  The following documentation should be helpful:
    http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/input_submit.asp

    Then tie this in with Abdullah's code.
    Tuesday, November 22, 2005 9:12 PM
  • Does anyone know what the equivelent of this code:

    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
    Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up...... ect.ect.ect.ect.ect.

    In visual basic express edition or any other express edition. anything would be helpful

    Wednesday, September 13, 2006 11:51 PM
  • The WinApi.cs code in the following article is a good example:

    http://www.eggheadcafe.com/articles/transparentform_send_mouse_click_to_desktop.asp

    Porting this to VB.NET is only straight forward if you are familiar with the DllImport portion.  Since that's what you are basically asking for help on...the important portion (the API call) ported...

    <DllImport(user32.dll)>

    Private Shared Sub Mouse_Event( _

     ByVal dwFlags As Integer, _

     ByVal dx As Integer, _

     ByVal dy As Integer, _

     ByVal dwData As Integer, _

     ByVal dwExtraInfo As IntPtr_

    )

     

    Often times the easiest way to get VB.NET help is to lean on the C# rather than the VB community.  An API tool in .NET similar to the one that we had in VB would be ideal.

    • Proposed as answer by ashchap Thursday, May 08, 2014 12:00 PM
    Sunday, September 17, 2006 2:48 AM
  • Hi Everyone,

    I know this is an old topic, but I reveived alot of emails on this subject since i wrote it more than one year ago, in fact, I'm still receiving messages on this subject. So let's solve this once and for all by posting the right declarations for the API. Thiw WILL WORK fine on VB 2003 and VB 2005:

    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

    I hope that helped

    Have fun people, and happy Ramadan

    Abdullah Al-Rasheed

    Saturday, October 07, 2006 4:05 AM
  • Hello.

    When i try your sample code i keep getting a noteice saying Declaration expected on the SetCursorPos statement.

    Would you have an idea why.

     

    Thank you.

     

    Saturday, March 22, 2008 9:25 PM
  • Asalaam w alaikum brother,

    I found your post on the mouse_events very helpful so I'm hopeful that you may be the one to help me here.

    The mouse_events works but it seems to require the window to be visible (not covered by anything). I want to send a mouse click to a window to which I have the handle and the coordinates of the desired spot to click but without interupting anything the user may be doing so it runs in the background.

    To further explain, the user is required to have a program running that occasionally brings up messages, these messages are unimportant to the user and though I've stopped it from bringing the messages to the foreground, they will continue to build up so when he/she closes what he/she is doing, there may be numerous messages to close.

    I want to close them when they are opened without inconveniencing the user.
    Using a hook, I am alerted as soon as the message is there even if hidden behind a full screen app.
    Some messages can be closed and others require a button to be clicked so I don't think destroywindow would be appropriate.
    I did try destroywindow for the ones that can just be closed but was unable to make it work even on an explorer window so my syntax must have been flawed.

    Is there a way to make the mouse_events method work on an underlying window?

    Thank you in advance for taking the time to read this and for any for help you may be able to provide.

    Allah hafiz

    Mateen Mahmood
    Wednesday, November 04, 2009 9:00 PM
  • Wednesday, November 04, 2009 9:05 PM
  • Assalamo Alaykom,

    I am new to coding and I keep having this issue (PInvokeStackImabalance) whenever I try to simulate a left click from a keyboard button. here is the code.

    -

    Public Class Iconform
    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.Click
    SettingsForm.Show()

    End Sub

    Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
    SettingsForm.Show()
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
    Me.Close()

    End Sub
    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    Dim bHandled As Boolean = False
    Dim xPos As Integer = Windows.Forms.Cursor.Position.X.ToString
    Dim zPos As Integer = Windows.Forms.Cursor.Position.Y.ToString

    Select Case e.KeyCode
    Case Keys.Right
    Windows.Forms.Cursor.Position = New Point(xPos + 10, zPos)
    Case Keys.Left
    Windows.Forms.Cursor.Position = New Point(xPos - 10, zPos)
    Case Keys.Down
    Windows.Forms.Cursor.Position = New Point(xPos, zPos + 10)
    Case Keys.Up
    Windows.Forms.Cursor.Position = New Point(xPos, zPos - 10)
    Case Keys.D
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)



    End Select
    End Sub
    End Class

     

    Thank you in advance.

                                                                                                                                                                                Mohammed Arabiat,

    Wednesday, July 07, 2010 6:37 PM
  • Hi, I see your post is nearly 2 years old, but I'll answer it anyway for people getting here by google searching (which I did).

    Simple answer is; If you have any trouble with the exception PInvokeStackImabalance, change the "As Long"s into "As Integer" :)

    Saturday, February 25, 2012 7:59 PM