如何计算和测量多个按键按下

huangapple go评论83阅读模式
英文:

How to count and measure multiple key presses

问题

我正在尝试开发一个用于游戏按键分析的工具,以便能够在游戏中不必花费1000多小时来制定正确的按键绑定。

这个想法是,程序应该在游戏过程中计算和测量按键的持续时间,最后输出统计信息,以查看哪些按键在游戏中按下的数量和持续时间最有用。然后,可以将最有用的按键重新绑定,使它们尽可能靠近手指,而不太有用的按键可以重新绑定得离手指更远。

我已经编写了一些代码来完成按键"A"和"W"的任务,但随着我添加更多按键,代码变得重复且庞大。

问题是,如何使用std::vector或std::array来简化我的代码,并一次为所有按键执行操作,以避免重复?(请原谅我的愚蠢,我已经阅读了多篇关于std::vector和std::array的文章,但仍然无法在我的代码中正确实现它们。)

这是我的代码,随着我添加更多按键,它变得越来越大:

英文:

I am trying to develop a tool for gaming for key analysis to be able to make the right keybindings without having to spend 1000+ hours in game.

The idea:
The program is supposed to count and measure key duration during gameplay and at the end output the stats to see which keys were the most useful according to their quantity and duration pressed in game. Afterwards the most useful keys can be rebinded as close as possible to reach and the less useful keys can be rebinded further away from fingers.

I have already wrote some code to accomplish this task for key "A" and "W", but as I add more keys the code is becoming repetitive and gigantic.

The question:
How can I use std::vector or std::array to simplify my code and do it for all keys at once to avoid repetitions? (Forgive me for my dumbness I have read multiple articles on std::vector and std::array, but still cannot properly implement them in my code.)

This is my code and as I add more keys it becomes larger and larger:

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

答案1

得分: 1

'A'键和'W'键的代码几乎相同,因此您可以创建一个包含所有需要处理的键的数组,然后使用for循环进行处理。像这样:

```cpp
const int keysNeedProcessCount = 2; // 例如,您需要处理2个键,'A'键和'W'键
char keysNeedProcess[keysNeedProcessCount] = {'A', 'W'};
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;
    }
}

<details>
<summary>英文:</summary>

Code for &#39;A&#39; key and &#39;W&#39; key is almost same, so you can have a array which contains all key you need processing, than use a for loop to do it. Like this:

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

for (int i = 0; i &lt; keysNeedProcessCount; i++) {
    if (GetAsyncKeyState(keysNeedProcess[i]) &amp; 0x8000)
    {
        if (!keysReleased[i])
        {
            ++keysCount[i];
            std::cout &lt;&lt; keysNeedProcess[i] &lt;&lt; &quot;_Times_Pressed: &quot; &lt;&lt; keysCount[i] &lt;&lt; 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 &lt;&lt; keysNeedProcess[i] &lt;&lt; &quot;_Hold_Duration [ms]: &quot; &lt;&lt; duration.count() * 1000.0f &lt;&lt; &quot;ms&quot; &lt;&lt; std::endl;
            std::cout &lt;&lt; keysNeedProcess[i] &lt;&lt; &quot;_Total_Hold_Duration [ms]: &quot; &lt;&lt; keysTotalDuration[i] &lt;&lt; &quot;ms&quot; &lt;&lt; std::endl;
        }
        keysReleased[i] = false;
    }
}

huangapple
  • 本文由 发表于 2023年2月26日 20:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定