c++ 如何计算和测量多次按键

wd2eg0qa  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(182)

我正试图开发一个工具,游戏的关键分析,能够使正确的键绑定,而不必花1000多个小时的游戏。
这个想法:该程序应该在游戏过程中计算和测量键的持续时间,并在结束时输出统计数据,看看哪些键是最有用的,根据他们的数量和持续时间按下游戏。之后,最有用的键可以重新绑定尽可能接近达到和不太有用的键可以重新绑定远离手指。
我已经为键"A"和"W"编写了一些代码来完成此任务,但随着我添加更多的键,代码变得重复和庞大。
问题是:如何使用std::vector或std::array来简化我的代码,并同时对所有键执行此操作以避免重复?(请原谅我的沉默,我已经阅读了许多关于std::vector和std::array的文章,但仍然无法在我的代码中正确实现它们。
这是我的代码,随着我添加更多的键,它变得越来越大:

#include <iostream>
#include <chrono>
#include <windows.h>

int main()
{
    std::chrono::time_point<std::chrono::steady_clock> start, finish;
    std::chrono::duration<float> duration;

    int aKeyCount{0};
    int wKeyCount{0};
    float akeyTotalDuration{0.0};
    float wkeyTotalDuration{0.0};
    bool aKeyReleased{false};
    bool wKeyReleased{false};

    while (true)
    {
//"A"-Key
        if (GetAsyncKeyState('A') & 0x8000)
        {
            if (!aKeyReleased)
            {
                ++aKeyCount;
                std::cout << "A_Times_Pressed: " << aKeyCount << std::endl;
                start = std::chrono::steady_clock::now();

            }
            aKeyReleased = true;
        }
        else
        {
            if(aKeyReleased)
            {
                finish = std::chrono::steady_clock::now();
                duration = finish - start;
                akeyTotalDuration += duration.count() * 1000.0f;
                std::cout << "A_Hold_Duration [ms]: " << duration.count() * 1000.0f << "ms" << std::endl;
                std::cout << "A_Total_Hold_Duration [ms]: " << akeyTotalDuration << "ms" << std::endl;
            }
            aKeyReleased = false;
        }
//"W"-Key
        if (GetAsyncKeyState('W') & 0x8000)
        {
            if (!wKeyReleased)
            {
                ++wKeyCount;
                std::cout << "W_Times_Pressed: " << wKeyCount << std::endl;
                start = std::chrono::steady_clock::now();

            }
            wKeyReleased = true;
        }
        else
        {
            if(wKeyReleased)
            {
                finish = std::chrono::steady_clock::now();
                duration = finish - start;
                wkeyTotalDuration += duration.count() * 1000.0f;
                std::cout << "W_Hold_Duration [ms]: " << duration.count() * 1000.0f << "ms" << std::endl;
                std::cout << "W_Total_Hold_Duration [ms]: " << wkeyTotalDuration << "ms" << std::endl;
            }
            wKeyReleased = false;
        }
    }

    return 0;
}
ozxc1zmp

ozxc1zmp1#

'A'键和'W'键的代码几乎是一样的,所以你可以有一个包含所有你需要处理的键的数组,而不是使用一个for循环来完成它。

const int keysNeedProcessCount = 2; // for example, you have 2 keys need processing, 'A' key and 'W' key
char keysNeedProcess[keysNeedProcessCount] = {'A', 'B'};
int keysCount[keysNeedProcessCount] = {0};
float keysTotalDuration[keysNeedProcessCount] = {0.0};
bool keysReleased[keysNeedProcessCount] = {false};

for (int i = 0; i < keysNeedProcessCount; i++) {
    if (GetAsyncKeyState(keysNeedProcess[i]) & 0x8000)
    {
        if (!keysReleased[i])
        {
            ++keysCount[i];
            std::cout << keysNeedProcess[i] << "_Times_Pressed: " << keysCount[i] << std::endl;
            start = std::chrono::steady_clock::now();
        }
        keysReleased[i] = true;
    }
    else
    {
        if(keysReleased[i])
        {
            finish = std::chrono::steady_clock::now();
            duration = finish - start;
            keysTotalDuration[i] += duration.count() * 1000.0f;
            std::cout << keysNeedProcess[i] << "_Hold_Duration [ms]: " << duration.count() * 1000.0f << "ms" << std::endl;
            std::cout << keysNeedProcess[i] << "_Total_Hold_Duration [ms]: " << keysTotalDuration[i] << "ms" << std::endl;
        }
        keysReleased[i] = false;
    }
}

相关问题