Boris
Boris

Reputation: 805

GetAsyncKeyState not working

I'm fairly new to C++ as I'm more experienced with Java/Python etc. But, I've been trying to implement a simple Trigger Bot, but added a failsafe so that if I press a certain key, the program will call the exit(0) method. But the way I implemented key input doesn't seem to work, could someone maybe help me out?

void MainScan(Contents scan) {
#if DB
    int debug = clock();
#endif
    while (true) {
        for (int y = scan.startY; y < scan.compareY; y++) {
            for (int x = scan.startX; x < scan.compareX; x++) {
                //SetCursorPos(x, y);
                if (GetAsyncKeyState(VK_DELETE)) {
                    exit(0);
                }
            }
        }
    }
}

Upvotes: 0

Views: 3790

Answers (1)

SenselessCoder
SenselessCoder

Reputation: 1159

Here's how you use it: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx

Here's a snippet of code from an old project of mine for a console based maze game:

Difficulty AskDifficulty() {
    // xy norm = 1, 2 y++ for rest
    point base(1, 2);
    drawatxy(1, 2, '*');
    while (GetAsyncKeyState(VK_RETURN)) // while it is being pressed, do not consider any input until we let go of the key
        g_delay(0.001);
    while (true) { // now we let go of it, consider inputs once more
        if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
            switch (base.y) {
            case 2:
                return DIFF_EASY;
            case 3:
                return DIFF_NORM;
            case 4:
                return DIFF_HARD;
            default:
                return DIFF_INVALID;
            }
        }
        else if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
            if (base.y < 4) {
                drawatxy(1, base.y, ' ');
                base.y++;
                drawatxy(1, base.y, '*');
                g_delay(0.125);
            }
        }
        else if (GetAsyncKeyState(VK_UP) & 0x8000) {
            if (base.y > 2) {
                drawatxy(1, base.y, ' ');
                base.y--;
                drawatxy(1, base.y, '*');
                g_delay(0.125);
            }
        }
        else
            _getch();
    }
}

Upvotes: 1

Related Questions