GetAsyncKeyState无效

时间:2016-01-23 21:09:47

标签: c++ input

我对C ++比较陌生,因为我对Java / Python等有更多的经验。但是,我一直试图实现一个简单的Trigger Bot,但是添加了一个故障保护,所以如果我按某个键,程序将调用exit(0)方法。但是我实施关键输入的方式似乎不起作用,有人可能会帮助我吗?

typedef

1 个答案:

答案 0 :(得分:1)

以下是您使用它的方式:https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx

以下是来自我的旧项目的代码片段,用于基于控制台的迷宫游戏:

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();
    }
}