c++ - 如何在不多次调用的情况下使用 GetAsyncKeyState?

标签 c++

这是我的代码目前的样子:

    if (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(0x31)) {
        //...
    }

    if (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(0x32)) {
        //...
    }

    if (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(0x33)) {
        //...
    }

    if (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(0x34)) {
        //...
    }

    if (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(0x35)) {
        //...
    }

是否有更有效的方法来执行此操作而无需在每个循环中多次调用 GetAsyncKeyState?也许将函数值存储为整数,然后使用 switch 语句?

此外,我不想使用 RegisterHotKey。

最佳答案

这里有一些提示。 0. 调用函数比直接使用值慢。 1. if-else 语句可以构建一棵树。这棵树应该通过路径压缩来优化。 您可以像这样宣传您的代码。

static bool keys[256];
int getkey(){          //Don't forget to call this  before querying the "keys" array.
    for(int i=0;i<256;i++)
          GetAsyncKeyState(i);
    return 0;
}
if(keys[VK_CONTROL])
{
    if (keys[0x31]) {
        //...
    }

    if (keys[0x32]) {
       //...
    }

    if (keys[0x33]) {
        //...
    }

    if (keys[0x34]) {
        //...
    }

    if (keys[0x35]) {
        //...
    }
}

关于c++ - 如何在不多次调用的情况下使用 GetAsyncKeyState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16682289/

相关文章:

c++ - 指针到指针到 Const 的转换

c++ - Visual Studio 输出 .obj 但不输出 .lib

c++ - 为什么我的程序不输出 i?

c++ - 在嵌套类中使用 struct vector 的成员

c++ - OpenCV 从网络摄像头流中分离剪影

c++ - 如何在 linux 中给线程发信号?

c++ - CMake 无法使用相对文件路径找到静态库

php - 在 javascript/HTML5 应用程序之外获取变量

c++ - 自处理器加电以来从 std::chrono::steady_clock::now() 获取微秒时间

c++ - 是否应该在成员对象上调用析构函数