Click here to Skip to main content
15,669,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);

        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

        enum SystemMetric
            SM_CXSCREEN = 0,
            SM_CYSCREEN = 1,
        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, //Left mousebutton down
            WM_LBUTTONUP = 0x202,  //Left mousebutton up
            WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
            WM_RBUTTONDOWN = 0x204, //Right mousebutton down
            WM_RBUTTONUP = 0x205,   //Right mousebutton up
            WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
            WM_KEYDOWN = 0x100,  //Key down
            WM_KEYUP = 0x101,   //Key up
            WM_CLICK = 0x00F5,
        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);

        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 --------------------------------//
Updated 9-May-16 3:47am

1 solution

According to documentation on the structure MOUSEINPUT, you can specify what action to take. The field dwFlags is the ORed combination of bits; if you use only MOUSEEVENTF_LEFTDOWN, only the left button down event will be simulated, the same with "up" events, and so on.

Please see MOUSEINPUT structure (Windows)[^].

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900