Simulating a mouse button click in Windows

10,874

Solution 1

This is in C#, but might help you to get an idea:

[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 void DoMouseClick()
   {
      int X = Cursor.Position.X;
      int Y = Cursor.Position.Y;
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
   }

Resources which might help:

http://msdn.microsoft.com/en-us/library/ms646260%28VS.85%29.aspx

http://www.codeguru.com/Cpp/W-P/win32/article.php/c4543/

Solution 2

Combining all togegher, here is the code for Qt:

#include <windows.h>
QApplication::desktop()->cursor().setPos(globalX,globalY);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 1, 1, 0, 0);

Solution 3

Lukas Šalkauskas' solution has worked for me for quite some time, but today it suddenly resulted in the following error (perhaps due to some .NET update by Windows Update?)

A call to PInvoke function 'SampleMethod' 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.

So I switched to the pinvoke.net sample code and it works well now:

[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

[Flags]
public enum MouseEventFlags
{
    LEFTDOWN = 0x00000002,
    LEFTUP = 0x00000004,
    MIDDLEDOWN = 0x00000020,
    MIDDLEUP = 0x00000040,
    MOVE = 0x00000001,
    ABSOLUTE = 0x00008000,
    RIGHTDOWN = 0x00000008,
    RIGHTUP = 0x00000010
}

public static void LeftClick(int x, int y)
{
    Cursor.Position = new System.Drawing.Point(x, y);
    mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
    mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
Share:
10,874
Nick Carlson
Author by

Nick Carlson

I always enjoy meeting other software developers. If you're in the Boulder area, drop me a line and we'll meet up for coffee.

Updated on July 21, 2022

Comments

  • Nick Carlson
    Nick Carlson almost 2 years

    I'm writing Remote Desktop clone in C++ using QT. So far I'm able to move the mouse cursor around fine. QT has a nice setPos function for that. However, I'm a bit lost as to what API/Library to use for simulating mouse button clicks.

    One method I'm aware of is to send the WM_(event) to a window using the window's HWND. However, I was hoping there was a more salient method for taking complete control over a mouse. Is there any other way to tell the operating system that the left mouse button has been clicked?

    Thanks.