如何找出在C 中按下键的时间
How to find out the amount of time for which a key is being pressed in c++?
我正在创建一个涉及我学校项目的弹性运动的游戏。我想通过确定按下某个键的时间来获取角度值的输入。然后,代码应给出等同于按键的时间的角度值。有任何建议或想法吗?
您需要一种以非阻止方式获取密钥状态的方法。
无法在标准的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.
使用密钥检测的方法取决于您如何制作游戏循环。如果可以接受几十毫秒的抖动,则只需在循环中添加时间戳检测而不担心太多。
相关文章:
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 如何找出引入AVX标志的内容
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 无法找出我的代码中的内存泄漏
- C++找出覆盖同一行的数组
- 找出可变参数宏中的__VA_ARGS__类型
- 我正在尝试根据 4 个方程找出六个值
- 从utsname找出Linux操作系统名称
- 试图找出为什么我会收到运行时错误?
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- UE4(С++) 我找出按下了哪个键
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 如何找出在C 中按下键的时间
- 找出当前时间是否是新秒的开始
- 如何找出在Visual Studio中编译C++标头需要多少时间
- 我有两个点和一个时间,我如何找出向量
- 在小于0 (N)的时间内找出点是否在N个(可能重叠)矩形中的一个内
- 找出运行一个c++函数所花费的时间
- 我如何找出编译器在哪里花费时间?
- 试图找出一个在特定时间内褪色的方程式