0
votes

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?

3
"the program will not continue because the left mouse button is not being held down anymore" I don't follow. - Lightness Races in Orbit
It is still being held down on the physical mouse, I was wondering how I could detect the physical mouse button being held down. - Ian
Don't send MOUSEEVENTF_LEFTUP. - Lightness Races in Orbit
It's part of what I'm doing. I need to send it. - Ian
For what reason? - Lightness Races in Orbit

3 Answers

1
votes

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
}
0
votes

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}

0
votes

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;
{