Sir Muffington
Sir Muffington

Reputation: 321

How do I simulate a single click on Windows and Linux using C?

What libraries should I use?

What documentation am I supposed to read?

Maybe someone could drop a code example?

Upvotes: 1

Views: 834

Answers (1)

Andreas Wenzel
Andreas Wenzel

Reputation: 25386

On Windows, if you want to simulate a mouse click inside a specific window, the relevant window messages are the following (link to documentation included):

WM_LBUTTONDOWN

WM_LBUTTONUP

WM_LBUTTONDBLCLK (on second click, this message is sent instead of WM_LBUTTONDOWN)

To simulate a mouse click, you can send a window any of these window messages, using the PostMessage() function.

For this function, you require a window handle (HWND) for the target window. This can be obtained for example using the EnumWindows() or FindWindow() function.

However, if you want to simulate a mouse click on the screen instead of inside a specific window, then you will have to use the SendInput() function. For further details, see this Stack Overflow question.

Also, be aware that since Windows Vista, for security reasons, it is not possible anymore to send processes with administrative privileges messages from non-privileged processes.

The above information only applies to Windows. Unfortunately, I can't help you with Linux, but you may find the solution in this Stack Overflow question.

Upvotes: 2

Related Questions