且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用SDL从键盘读取输入的***方法是什么?

更新时间:2023-11-27 08:27:46

我喜欢做1的变体,在其中填充三个数组,不仅指示当前状态,还指示哪些键刚刚按下,哪些键刚刚按下.上升了.这使我可以轻松地在代码中检查那些事件(无需与以前的快照进行比较),但是最重要的是,它不会错过持续时间少于一帧的事件.例如,如果您的游戏由于机器速度慢而以10 fps的速度运行,则用户可能在两次更新例程调用之间按下并释放了一个重要的键,然后您的系统将永远不会对其进行注册.这非常令人沮丧.

I prefer to do a variation of 1, where I fill three arrays, indicating not only the current state, but also which keys just went down and which keys just went up. This allows me to easily check for those events in code (without comparing to the previous snapshot), but, most importantly, it won't miss events that last less than a frame. For example, if your game is running at 10 fps due to a slow machine, the user might press and release an important key between two calls of your update routine, and then your system will never register it. This is extremely frustrating.

当按下键时,SDL还发送键事件,这使您可以在每次按下键时具有多个键按下事件.我发现在通过项目列表实现键盘滚动时,这特别有用.键盘控制的菜单.

SDL also sends key events when the key is held down, which allow you to have multiple key down events for each key up. I find this particularly useful when implementing keyboard scrolling through a list of items, e.g. a keyboard-controlled menu.