Search code examples
c++windowskeyboardgetch

Is there a way to check for shift held down THEN control held down for each input?


I'm trying to use _getch to get keyboard strokes for incrementing money in a game. If I want the money to be incremented faster, in 10's while shift is held down and 100's control is held down what would be the best way to do that? If I just check for shifts it's easy enough, but if I want to check for a shift then a control afterwards it won't work.

For instance checking to increment by 1 or 10 I'd do something like this

key = _getch();
if (key == 224)
{
    key = _getch();
    if (key == 75) // left arrow
    {
        if (GetAsyncKeyState(VK_SHIFT)
            for (int i = 0; i < 10; i++) // go left 10 times
                    left();
        else
            left();
    }
}

If I add an else if for control for 100's it will NOT work. Most likely because running the GetAsyncKeyState fucntion again makes it not read the key state properly.

else if (GetAsyncKeyState(VK_CONTROL)
    for (int i = 0; i < 100; i++) // go left 100 times
        left();

Any possible workaround? Thanks!


Solution

  • Turns out control prevent the console from "flushing". So, it was just a problem with control itself causing the error, I decided to use while-shift held for 10's and while-tab held for 100's. If there is a way to use alt, which I don't think there is, I'd use that. This is just a typical case of me being stupid and taking like 3 hours to realize it was the keybind interacting with the cmd prompt causing the issue.