Kilo King
Kilo King

Reputation: 309

Stuck In C ++ While Loop

When I execute this program and I hold down the left mouse button it puts me in the while loop. Now that is all nice and dandy, which I want, however it seems to leave me stuck in the while loop with no exit.

Now I'm questioning why am I getting stuck in this while loop when I let go of the LButton. Because when I let go of the LButton shouldn't the statement return false?

if (GetAsyncKeyState(0x01)){
        Sleep(250);
        while (GetAsyncKeyState(0x01)){
            /* code */
            Sleep(100);
        }
    }

edit:

alright so below is the same statement but with the shift key instead of the LButton. It executes perfectly fine. However now when I change it to the LButton it get's stuck in the loop.

if (GetAsyncKeyState(0x10)){
        while (GetAsyncKeyState(0x10)){
            left_click();
            clicks++;
            Sleep(delay);
        }
        display_values(delay, clicks);
    }

I did try the function

bool IsLeftMouseButtonPressed() { return GetAsyncKeyState(VK_LBUTTON) & 0x8000; }

solved the problem of being stuck in the loop, however now it only executes the commands inside the while loop once. Which I'm aiming for continuous until the user lets go of the LButton.

Upvotes: 1

Views: 371

Answers (1)

marcinj
marcinj

Reputation: 50036

From msdn for this api:

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.

so what you are after is a :

bool IsLeftMouseButtonPressed() { return GetAsyncKeyState(VK_LBUTTON) & 0x8000; }

this function will return true if left mouse button is down currently

Upvotes: 1

Related Questions