How to simulate mouse clicks without moving the cursor position first?
I have a program that perform mouse click on foreground window, in which I am using SendInput or SendMessage to emulate left mouse click. The issue is, that if I set cursor to clicking position will make the click, however if I don't set cursor than no click happens even trough I do pass x and y points. I would like to perform left mouse click without the need of actually moving the cursor at all.
Thank you in advance.
What I have tried:
// ------------------------------- SendInput --------------------------------//
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[Flags]
enum MouseEventFlags : uint
{
MOUSEEVENTF_MOVE = 0x0001,
MOUSEEVENTF_LEFTDOWN = 0x0002,
MOUSEEVENTF_LEFTUP = 0x0004,
MOUSEEVENTF_RIGHTDOWN = 0x0008,
MOUSEEVENTF_RIGHTUP = 0x0010,
MOUSEEVENTF_MIDDLEDOWN = 0x0020,
MOUSEEVENTF_MIDDLEUP = 0x0040,
MOUSEEVENTF_XDOWN = 0x0080,
MOUSEEVENTF_XUP = 0x0100,
MOUSEEVENTF_WHEEL = 0x0800,
MOUSEEVENTF_VIRTUALDESK = 0x4000,
MOUSEEVENTF_ABSOLUTE = 0x8000
}
enum SendInputEventType : int
{
InputMouse,
InputKeyboard,
InputHardware
}
enum SystemMetric
{
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
}
[DllImport("user32.dll")]
static extern int GetSystemMetrics(SystemMetric smIndex);
public static void ClickLeftMouseButton(Point p)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = (p.X * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
mouseInput.mkhi.mi.dy = (p.Y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
mouseInput.mkhi.mi.mouseData = 0;
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN | MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP | MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}
// ------------------------------- SendInput --------------------------------//
// ------------------------------- SendMessage --------------------------------//
public enum WMessages : int
{
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_LBUTTONDBLCLK = 0x203,
WM_RBUTTONDOWN = 0x204,
WM_RBUTTONUP = 0x205,
WM_RBUTTONDBLCLK = 0x206,
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WM_CLICK = 0x00F5,
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string sClassName, string sAppName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
private void button1_Click(object sender, EventArgs e)
{
Point pt = new Point(263, 88);
IntPtr handle = WindowFromPoint(pt.X, pt.Y);
SendMessage(this.Handle, (int)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X, pt.Y));
}
// ------------------------------- SendMessage --------------------------------//