C ++多重按键输入或按键按下/释放事件 [英] C++ mutiple key input or key press/release events
问题描述
我正在用C ++编写游戏.为此,我需要检测同时按下或按住的多个键,或者检测按键和释放键的事件.
I am writing a game in C++. For it, I need to detect multiple keys being pressed or held at the same time, or detect key press and key release events.
例如,如果我同时按住 up 和 right 键,我希望能够检测到并沿对角线移动游戏.另外,当前如果我按住 right 键,并按 up ,则它不再识别出正在按住 right 的状态.我需要一种方法来找到真正释放 right 键的时间,这样它就不会像这样被打断.
For example, if I hold both the up and right keys, I want to be able to detect this and move the game diagonally. Additionally, currently if I am holding the right key, and I press up, it no longer recognizes that right is being held. I need a way to find when the right key is actually released so it isn't interrupted like this.
我知道 sdl2
可以识别正在按下或按住的多个键,但这需要创建一个窗口,并且我希望它完全在终端中.
I know that sdl2
can recognize multiple keys being pressed or held, but that requires creating a window, and I want this to be entirely within the terminal.
getch()
对此无效.
我可以用来做什么?如果可能的话,我想避免使用Allegro.
What can I use for this? I'd like to avoid Allegro if possible.
推荐答案
您没有提到您现在如何读取按键输入以及如何处理输入,因此这只是有关如何处理按键输入的一般建议.
You don’t mention how you are reading key input now and how you are handling the input so this is just a general advice on how to handle key input.
当您检测或处理特定按键的按键按下(按键按下)事件时,请设置一个标志(例如keyflags = keyflags | KEY_RIGHT)以指示按键已按下,并取消设置(删除)keyup事件上的标记.如果您直接按按键操作,这可能会解决您今天遇到的问题.
When you detect or handle a key pressed (key down) event for a specific key, set a flag (e.g. keyflags = keyflags | KEY_RIGHT) to indicate the key is down, and unset (delete) the flag on keyup event. This might solve the problem you experience today if you act directly on key presses.
这篇关于C ++多重按键输入或按键按下/释放事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!