简体   繁体   中英

Need help creating a Loop which sleeps a condition, when condition is met it stops until the next action

I'm a very beginner at C++ and I'm trying to simulate mouse clicks to a program using C++.

What I'm trying to reach is the following:

While the right mouse button is being pressed:

User right clicks the mouse button (first action)

The application Sleeps for a few milseconds (second action)

It stops sleeping and goes to another condition (third action)

It stays in that condition while the user leaves the mouse click (fourth action)

(Repeat while user clicks again on the right mouse button)

I've tried some alternatives but none of them seems to be working as I expect. Right now I've tried the following:

bool sleep = false;

while (sleep = false) {
    if (MK_RBUTTON) {
        Sleep(100);
        sleep = true;
    }
}
if (sleep = true) {
    (third action);
}

As @Remy Lebeau said, GetAsyncKeyState () is a good choice.

I tried to change your code, as follows

#include <Windows.h>
#include <iostream>
using namespace std;

void third_action();
int main()
{
    BOOL sleep = FALSE;
    while (sleep == FALSE)
    {
        if (GetAsyncKeyState(VK_RBUTTON) < 0)
        {
            //Click RightButton
            Sleep(600);
            sleep = TRUE;
            cout << "Click RightButton" << endl;
        }
        if (sleep == TRUE)
        {
            //third action
            third_action();
            sleep = FALSE;
        }
    }
    return 0;
}

void third_action()
{
    /*
    Execute your third action here. 
    */
    cout << "third action worked" << endl;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM