This is a wrapper function for sending a left-click event in windows. It can be called by using leftclick() instead of having to set these variables every time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| #include <Windows.h>
using namespace std;
void leftclick()
{
PINPUT click = new INPUT;
click->type = INPUT_MOUSE;
click->mi.dwFlags = 0x2;
click->mi.mouseData = 0;
click->mi.time = 0;
click->mi.dwExtraInfo = 0;
SendInput(1, click, sizeof(INPUT));
click->mi.dwFlags = 0x4;
SendInput(1, click, sizeof(INPUT));
}
|
Line 7/8 set up the PINPUT structure for use.
Lines 9-12 set additional parameters on the struct (dwFlags=2 is the "down" event).
Line 13 sends the "down" event, line 14 sets it to send an "up", and 15 sends it again.
To make an auto-clicker for idle/clicker games, the following works just fine:
1
2
3
4
5
| while(1)
{
leftclick();
Sleep(20);
}
|
You can easily change the left-click to right-click by changing the dwFlags parameter. The MSDN page https://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx shows the values for dwFlags for each mouse action.
Another thing you could do is create a function called leftdown(), and another called leftup(), and remove lines 14 and 15 from the above function to create one that holds the mouse button down. This can be paired with SetCursorPos() to click and drag to screen coordinates.