I need to simulate a mouse click, but I need it to simulate a click that will effect the open window (In this case the browser). Is there an easy way to do this or is it not possible? Any help is appreciated.

5 Years
Discussion Span
Last Post by nmaillet

Do you just need the browser window to "take focus" or does something else need to happen.
From what kind of app are you starting (another browser, console app, win forms, etc.)?


You could use the SendInputs() function in the WinAPI. This function does not target particular windows however. First thing you'll need is structure definitions:

struct INPUT
        public uint type;
        public MOUSEINPUT mi;

    struct MOUSEINPUT
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;

In whatever class you'd be using it, declare the external function:

private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

I did a quick test to move the cursor relative to its current position:

SendInput(0, null, Marshal.SizeOf(typeof(INPUT)));

            INPUT input = new INPUT();
            input.type = 0x0000;

            MOUSEINPUT mouseInput = new MOUSEINPUT();
            mouseInput.dx = 300;
            mouseInput.dy = 150;
            mouseInput.mouseData = 0;
            mouseInput.dwFlags = 0x0001;
            mouseInput.time = 0;
            mouseInput.dwExtraInfo = IntPtr.Zero;

            input.mi = mouseInput;

            INPUT[] inputArray = new INPUT[] { input };

            SendInput((uint)inputArray.Length, inputArray, Marshal.SizeOf(typeof(INPUT)));

Take a look at these references to modify it:
SendInput() Function
INPUT Structure

Edited by nmaillet: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.