How to Simulate a Mouse Click at a Certain Position on the Screen

How can I simulate a mouse click at a certain position on the screen?

Here's a code that is using unmanaged functions to simulate mouse clicks :

//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;

//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}

To keep the mouse pressed for a specific duration you can Sleep() the thread that is executing this function, for example :

mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);

The above code will keep the mouse pressed for 1 second unless the user presses the releases the mouse button. Also, make sure to not execute this code on the main UI thread as it will cause it to hang.

Simulate mouse clicks at a certain position on inactive window in C#

Yes it is possible, here's the code I used for a previous school project:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

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

//This simulates a left mouse click
public static void LeftMouseClick(Point position)
{
Cursor.Position = position;
mouse_event(MOUSEEVENTF_LEFTDOWN, position.X, position.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, position.X, position.Y, 0, 0);
}

EDIT : It seems that the mouse_event function was replaced by SendInput() but it still works (Windows 7 and earlier)

Simulate mouse clicks at a certain position on inactive window in Java?

java.awt.Robot

Clicking into an active window is going to activate it, though.

How to simulate a click by using x,y coordinates in JavaScript?

You can dispatch a click event, though this is not the same as a real click. For instance, it can't be used to trick a cross-domain iframe document into thinking it was clicked.

All modern browsers support document.elementFromPoint and HTMLElement.prototype.click(), since at least IE 6, Firefox 5, any version of Chrome and probably any version of Safari you're likely to care about. It will even follow links and submit forms:

document.elementFromPoint(x, y).click();
  • https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
  • https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click


Related Topics



Leave a reply



Submit