Reputation: 11
I am writing a program that scans if the Left Mouse Button is held down, then sends a left mouse button up and continues after that. The problem is, since I am sending a left mouse button up, the program will not continue because the left mouse button is not being held down anymore.
Here is some pseudo:
if(GetKeyState(VK_LBUTTON) < 0){
Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
//Rest of code
}
How can I detect the left mouse button being down after this? Would I have to use a driver?
Upvotes: 0
Views: 2063
Reputation: 566
From reading your description of the program only here is my implementation. Using the Windows API:
while (true) {
//check if left mouse button is down
if (GetKeyState(VK_LBUTTON) & 0x8000) {
//send left mouse button up
//You might want to place a delay in here
//to simulate the natural speed of a mouse click
//Sleep(140);
INPUT Input = { 0 };
::ZeroMemory(&Input, sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1, &Input, sizeof(INPUT));
}
}
You can place this code into a function that you call in a thread if you want to do other things while you forcibly stop people from clicking and dragging.
void noHoldingAllowed() {
//insert code here used above...
}
int main(void) {
std::thread t1(noHoldingAllowed);
//other stuff...
return 0;
{
Upvotes: 0
Reputation: 3305
Just use a flag:
bool lButtonWasDown=flase;
if(GetKeyState(VK_LBUTTON) < 0){
Sleep(10);
lButtonWasDown = true;
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
}
if (lButtonWasDown)
{
// rest of the code for lButtonWasDown = true
}
Upvotes: 1
Reputation: 708
I haven't tested this code, but it should work
while(programIsRunning)
{
if(GetKeyState(VK_LBUTTON) < 0){
Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
// rest of the code
}
It should work since if you have the Lmouse button held while the loop reruns if will trigger the if statement which will then trigger a mouseup event.
note:
You might be able to do while(GetKeyState(VK_LBUTTON) < 0){//code here}
Upvotes: 0