How can I make a mouse input with SendInput()? I am entirely confused, I don't understand what it is saying on Microsoft's website. Here is my current code:
if (toggled && (GetKeyState(VK_LBUTTON) & 0x100))
{
SendInput(UINT, MOUSEINPUT, WM_LBUTTONUP, NULL);
Sleep((rand() % 1000 / cps));
}
You need to fill out an (array of) INPUT
struct(s) containing the mouse details you want, and then you pass that array to SendInput()
. This is pretty clearly explained in the SendInput()
documentation :
Parameters
cInputs
Type:
UINT
The number of structures in the
pInputs
array.
pInputs
Type:
LPINPUT
An array of
INPUT
structures. Each structure represents an event to be inserted into the keyboard or mouse input stream.
cbSize
Type:
int
The size, in bytes, of an
INPUT
structure. IfcbSize
is not the size of anINPUT
structure, the function fails.
Also, your call to GetKeyState()
is wrong, as it will never return a SHORT
value that has its 9th bit set to 1. Per the GetKeyState()
documentation , only the low (1st) bit or the high (16th) bit will ever be set to 1:
The return value specifies the status of the specified virtual key, as follows:
- If the high-order bit is 1, the key is down; otherwise, it is up.
- If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
So, if your goal is to check if the left mouse button is currently held down, you need to use & 0x8000
(or < 0
) instead of & 0x100
(even better, consider using GetAsyncKeyState()
instead). See Check if left mouse button is held down? .
Try this instead:
if (toggled && (GetKeyState(VK_LBUTTON) < 0 /*& 0x8000*/))
{
INPUT input = {};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(INPUT));
Sleep((rand() % 1000 / cps));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.