如何找出在C 中按下键的时间

How to find out the amount of time for which a key is being pressed in c++?

本文关键字:时间 何找出      更新时间:2023-10-16

我正在创建一个涉及我学校项目的弹性运动的游戏。我想通过确定按下某个键的时间来获取角度值的输入。然后,代码应给出等同于按键的时间的角度值。有任何建议或想法吗?

您需要一种以非阻止方式获取密钥状态的方法。

无法在标准的C 库中实现这一目标,因此您需要使用某种图书馆/框架,其中有很多。

如果您使用的是Windows,一种方法是直接与OS API接口,该操作系统从Windows队列中直接从应用程序中直接关键事件。

这是应用程序等待三秒钟的一个小示例,然后打印出这三秒钟内发生的关键事件以及生成事件的密钥。

#include <stdio.h>
#include <iostream>
#include <vector>
#include <windows.h>
#include <thread>
#include <chrono>
#define MAX_INPUT_EVENTS 100
int main()
{
    //Handle to console input buffer
    HANDLE g_console_handle;
    //Return dword
    DWORD ret, ret_aux;
    //Input event structure
    INPUT_RECORD input_event[ MAX_INPUT_EVENTS ];
    //return flag
    bool f_ret;

    std::cout << "press some keys while the process waits 3 seconds...n";
    std::this_thread::sleep_for( std::chrono::milliseconds(3000) );
    //get handle to console
    g_console_handle = GetStdHandle( STD_INPUT_HANDLE );
    //Get number of pending input events
    f_ret = GetNumberOfConsoleInputEvents( g_console_handle, &ret );
    //if: fail
    if (f_ret == false)
    {
        std::cerr << "ERR: could not get number of pending input eventsn";
        return true; //Fail
    }
    //if: at least one event has been detected
    if (ret > 0)
    {
        //if: above processing limits
        if (ret >= MAX_INPUT_EVENTS)
        {
            std::cerr << "ERR: too many input eventsn";
            return true; //Fail
        }
        //Get all the input event
        f_ret = ReadConsoleInput
        (
            g_console_handle,
            input_event,
            ret,
            &ret_aux
        );
        //for: every input event
        for (DWORD t = 0;t < ret_aux; t++)
        {
            //switch: Decode event type
            switch(input_event[t].EventType)
            {
                //case: keyboard
                case KEY_EVENT:
                {
                    //Structure holding key event
                    KEY_EVENT_RECORD &event = input_event[t].Event.KeyEvent;
                    //List of virtual keys
                    //https://learn.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes
                    //true=down, pressed
                    if (event.bKeyDown == true)
                    {
                        std::cout << "Virtual key: " << event.wVirtualKeyCode << " is downn";
                    }
                    //false = up, released
                    else
                    {
                        std::cout << "Virtual key: " << event.wVirtualKeyCode << " is upn";
                    }
                    break;
                }
                //unhandled input event
                default:
                {
                    //do nothing
                }
            }   //end switch: Decode event type
        }   //end for: every input event
    }   //end if: at least one event has been detected
    //if: no event detected
    else
    {
    }
}

这是输出,列表

press some keys while the process waits 3 seconds...
Virtual key: 68 is down
Virtual key: 65 is down
Virtual key: 68 is up
Virtual key: 65 is up
Virtual key: 68 is down
Virtual key: 68 is up
Virtual key: 65 is down
Virtual key: 70 is down
Virtual key: 87 is down
Virtual key: 70 is up
Virtual key: 65 is up
Virtual key: 70 is down
Virtual key: 87 is up
Virtual key: 70 is up
Process returned 0 (0x0)   execution time : 3.046 s
Press any key to continue.

使用密钥检测的方法取决于您如何制作游戏循环。如果可以接受几十毫秒的抖动,则只需在循环中添加时间戳检测而不担心太多。