none
How can I do virtual mouse click?

    Question

  • Hi all,

    I have to do a virtual click on any position on window(I mean click by not use a mouse  ). How can I do that in C#?

    Thank a lot.

    Wednesday, May 10, 2006 9:30 AM

Answers

  • You can simulate a mouse click by calling the mouse_event API.

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;
    public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    public const int MOUSEEVENTF_RIGHTUP = 0x10;

    public void MouseClick()
    {
    int x = 100;
    int y = 100;

    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }

    Change the values of x and y to simulate a click elsewhere on th screen

    Wednesday, May 10, 2006 11:23 AM

All replies

  • You can simulate a mouse click by calling the mouse_event API.

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;
    public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    public const int MOUSEEVENTF_RIGHTUP = 0x10;

    public void MouseClick()
    {
    int x = 100;
    int y = 100;

    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }

    Change the values of x and y to simulate a click elsewhere on th screen

    Wednesday, May 10, 2006 11:23 AM
  • Thanks a lot De Bont,

    This code is very helpful  .

    Thursday, May 11, 2006 2:15 AM
  • Nobita,

    Can you mark the reply as an answer please. I makes it easier for others to find answers that way.

    Thursday, May 11, 2006 5:59 AM
  • Hi De Bont,

    I throught I marked it if it haven't been marked yet, please suggest me how to mark that reply.

    Friday, May 19, 2006 2:42 AM
  • Has anyone got this code in VB? VB Express 2005 prefreably but any will do
    Monday, August 07, 2006 10:09 PM
  • I've never used VB.NET (just VB6), but try this:

    Declare Auto Function mouse_event Lib "user32.dll" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer) As Integer

    Const MOUSEEVENTF_LEFTDOWN as Integer = 2
    Const MOUSEEVENTF_LEFTUP as Integer = 4
    Const MOUSEEVENTF_RIGHTDOWN as Integer = 8
    Const MOUSEEVENTF_RIGHTUP as Integer = 16

    Public Shared Sub MouseClick()
    {
        Dim x As Integer = 100
        Dim y As Integer = 100;

        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    }


    Tuesday, August 08, 2006 12:26 AM
  • My interest is to send a click event to a specific control, a Button for an example ^^

    I'm pretty sure it's easy because it is in any other GUI API I used, but I have to admit Windows.Form assembly disappoint me sometime...

    Anyway I'll probably find the solution alone and thanks a lot for the tip to simulate a click anywhere in the screen !!!

     

    Wednesday, September 13, 2006 5:47 PM
  • To click button programmatically use PerformClick() method:
    myButton.PerformClick();
    Wednesday, September 13, 2006 7:39 PM
  • Hi,

    I tried to find the PerformClick method in the System.Windows.Forms.Button object but with no success.
    This method probably exist in the .NET framework 2.0 version of the Button Class but not in the compact framework one.
    Thanks anyway for the tip !

    See you boban
    Thursday, January 11, 2007 3:15 PM
  • Hi Sven de Bond,

    Cane you telle me how to do a virtual mouse click in Visual Basic?

     

    nicknehoux@hotmail.com

    Friday, May 18, 2007 2:58 PM
  • I answered this question already, if you would read above.  If you already saw this, please let us know specifically what part you're having trouble with.
    Tuesday, May 22, 2007 2:12 AM
  • Sirjis,

     

    The example you posted is a mix between VB.NET & C# code and will not work.

     

    Try this in VB.NET

     

         Public Const MOUSEEVENTF_LEFTDOWN As Integer = 2
        
        
    Public Const MOUSEEVENTF_LEFTUP As Integer = 4
        
        
    Public Const MOUSEEVENTF_RIGHTDOWN As Integer = 8
        
        
    Public Const MOUSEEVENTF_RIGHTUP As Integer = 16
        
        
    Public Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As IntegerByVal dx As IntegerByVal dy As IntegerByVal cButtons As IntegerByVal dwExtraInfo As Integer)
        
        
    Public Sub MouseClick()
            
    Dim As Integer = 100
            
    Dim As Integer = 100
            
    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 00)
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 
    00)
        
    End Sub

    Tuesday, May 22, 2007 12:13 PM
  •  

    How do you use this? I think I know how to add it but how do you assign it to like a button press?
    Tuesday, January 15, 2008 1:14 PM
  • You give the MouseClick method a screen coordinate that will hit the button you are interested.

    Tuesday, January 15, 2008 10:36 PM
  • Can someone explain to me what the last 2 zeroes at the end are for?

    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)

    I understand that the x and y are the coordinates of where the click will happen, but what about the 2 0's?

    Sunday, October 17, 2010 7:23 PM
  • Sunday, October 17, 2010 8:47 PM
  • This code will use your mouse, not a virtual mouse,

    what to change so it will do it with a virtual mouse?

    Saturday, December 11, 2010 3:10 PM
  • Dear All,

    I tried this for my requirement and worked like a charm.

    Excellent code. Thanks a ton for sharing this code snippet.

    Thank You

    Guru


    Regards, Guru

    Sunday, September 01, 2013 9:02 AM