c++ - GetAsyncKeyState 不工作

标签 c++ input

我是 C++ 的新手,因为我对 Java/Python 等更有经验。但是,我一直在尝试实现一个简单的 Trigger Bot,但添加了一个故障保护,这样如果我按下某个键,程序将调用 exit(0) 方法。但是我实现按键输入的方式似乎不起作用,有人可以帮助我吗?

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

最佳答案

使用方法如下: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();
    }
}

关于c++ - GetAsyncKeyState 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34968895/

相关文章:

c++ - 如何对多维数组进行排序?

c++ - C++ (GCC) 中的 C99 严格别名规则

c++ - 将库添加到链接器

angularjs - angular ng-model - 如何从输入字段中只读取空格字符

javascript - 输入类型=范围上的 onchange 事件在拖动时未在 Firefox 中触发

arrays - 使用 Groovy 从文件中读取字符串并将它们放入数组中

c++ - Cout 使用运算符 << 调用方法

c++ - vs2015 C++ 包含目录 gstreamer

c++ - 将文件读入程序

input - SDL 2.0 键盘输入问题