Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a Virtual Mouse click C# without using a mouse [duplicate]

I'd like to perform a click in a windows application, without using the real mouse (so I can minimize it). Much like a bot would behave.

How would I do this?

like image 724
Frunk Avatar asked Feb 28 '13 21:02

Frunk


People also ask

How do you simulate a mouse click?

You can right-click by holding down the left mouse button.

How do you simulate mouse click in C#?

Some controls, like Button in System. Windows. Forms, have a "PerformClick" method to do just that. If the control doesn't have a PerformClick method, you can extend/add one, just needs to call OnMouseClick with a suitable MouseEventArgs argument.

How do I simulate a mouse click on my website?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


1 Answers

I think the function you're looking for is PostMessage

[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

You can read more about it here on codeproject, and download a demo project, which sends keystrokes.

This method posts messages directly on the input queue associated with the program, based on the process handle you use (hWnd)

You can also use this function to send mouse clicks with it, by posting button events, like so:

PostMessage(hWnd, WM_LBUTTONDBLCLK, 0, l);

More information about those button events can be found here on MSDN.

I'm sure if you search around the internet for samples for PostMessage mouse events you'll find plenty

like image 59
Ron Sijm Avatar answered Sep 22 '22 14:09

Ron Sijm