7

How could I programmatically trigger a left-click event on the mouse?

Thanks.

edit: the event is not triggered directly on a button. I'm aiming for the Windows platform.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Alex
  • 7,432
  • 20
  • 75
  • 118
  • 2
    In which platform? Winforms? WPF? Silverlight? webforms(=html/javascript)? Or just "windows" (low level) – Marc Gravell Apr 29 '10 at 11:49
  • Possible duplicate of [How to simulate Mouse Click in C#?](http://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c) – Alex May 16 '17 at 11:46

3 Answers3

13

To perform a mouse click:

 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

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

        public static void DoMouseClick()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }

To move the cursor where you want:

[DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        public static void MoveCursorToPoint(int x, int y)
        {
            SetCursorPos(x, y);
        }
JohnForDummies
  • 950
  • 4
  • 6
  • 2
    I saw this one, tried it, but the mouse_event function is obsolete. I'm trying to do the same thing with SendInput now. – Alex Apr 29 '10 at 12:41
  • 1
    In addition to mouse_event being superseded by SendInput as Andrei pointed out, you can pass the mouse position by setting the MOUSEEVENTF_ABSOLUTE mask in the first argument and passing the x and y coords in the second and third args, rather than calling SetCursorPos first. – rob Nov 23 '16 at 19:26
  • I will try with this method since(as commented on the sendinput answer), SendInput didnt work yet for me. With this method I got an error `Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Use.....\Debug\hprog.vshost.exe'. Additional information: A call to PInvoke function 'prog1!prog1.FrmBlah::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.` – barlop May 15 '17 at 13:11
8

If it's right on a button, you can use

button1.PerformClick();

Otherwise, you can check out this MSDN article which discusses simulating mouse (and keyboard) input.

Additionally, this project may be able to help you out as well. Under the covers, it uses SendInput.

NG.
  • 22,560
  • 5
  • 55
  • 61
  • It's odd to me that Java seems to have better built-in support than C# for easily clicking the mouse in Windows at an arbitrary location, but the InputSimulator library is just what I was looking for. You should make that more prominent in your answer. – rob Nov 23 '16 at 21:30
1

https://web.archive.org/web/20140214230712/http://www.pinvoke.net/default.aspx/user32.sendinput

Use the Win32 API to send input.

Update:

Since I no longer work with Win32 API, I will not update this answer to be correct when the platform changes or websites become unavailable. Since this answer doesn't even conform to Stackoverflow standards (does not contain the answer itself, but rather a link to an external, now defunct resource), there's no point giving it any points or spending any more time on it.

Instead, take a look at this question on Stackoverflow, which I think is a duplicate:

How to simulate Mouse Click in C#?

Community
  • 1
  • 1
Alex
  • 14,338
  • 5
  • 41
  • 59
  • I find that doesn't work e.g. the visual studio debugger puts a line under `UINT` and under `INPUT[]` I see there is no such class as UINT though I can change that to UInt32 But I don't what the equivalent change would be to INPUT[] to make it work – barlop May 15 '17 at 13:08
  • @barlop thanks for digging up this relic :) and I'm sorry that I can't help you with this. I marked it as deprecated. Seek out better answers, for ex. here http://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c – Alex May 16 '17 at 11:46
  • You're understating the validity of your answer Andrei has said that the mouse_event solution is obsolete and your SendInput solution is more up to date.. And in the question you linked to, the most highly rated answer has the mouse_event solution.. So your answer may well be more up to date than the one there. And just because a good website, pinvoke has been hijacked or is redirecting to redgate software company, doesn't mean the information on the webpage is obsolete. In fact the main page of the pinvoke website at the moment says down for maintenance. – barlop May 16 '17 at 12:46