有效的键盘输入处理
实现键盘处理的好方法是什么? 在任何语言中,当我编写键盘交互程序(例如俄罗斯方块游戏)时,我最终都会得到一些如下所示的代码:(
for event in pygame.event.get():
if event.type == KEYDOWN:
if False: pass #make everything an elif
elif rotating: pass
elif event.key == K_q:
elif event.key == K_e:
elif event.key == K_LEFT:
curpiece.shift(-1, 0)
shadowpiece = curpiece.clone(); setupshadow(shadowpiece)
elif event.key == K_RIGHT:
curpiece.shift(1, 0)
shadowpiece = curpiece.clone(); setupshadow(shadowpiece)
缩短)。 我不喜欢这个,因为这必须进入我的主循环,并且它会扰乱程序的所有部分。 这也使得用户无法在配置屏幕上更改哪个键映射到哪个操作。 是否有一个好的模式可以使用某种形式的函数回调来做到这一点?
What is a good way to implement keyboard handling? In any language, where I write a keyboard-interactive program (such as a tetris game), I end up having some code that looks like this:
for event in pygame.event.get():
if event.type == KEYDOWN:
if False: pass #make everything an elif
elif rotating: pass
elif event.key == K_q:
elif event.key == K_e:
elif event.key == K_LEFT:
curpiece.shift(-1, 0)
shadowpiece = curpiece.clone(); setupshadow(shadowpiece)
elif event.key == K_RIGHT:
curpiece.shift(1, 0)
shadowpiece = curpiece.clone(); setupshadow(shadowpiece)
(shortened). I don't like this, as this has to go in my main loop, and it messes with all parts of the program. This also makes it impossible to have a user config screen where they can change which key maps to which action. Is there a good pattern to do this using some form of function callbacks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个字典,其中键是输入,值是处理按键的函数:
然后更改键就是修改字典的键。
You could create a dictionary where the keys are the input and the value is a function that handles the keypress:
Then changing the keys is a matter of modifying keys of the dictionary.
除了superjoe30的答案之外,您还可以使用两级映射(两个字典)
这将使用户定义映射变得更容易。 即用户可以将其按键映射到“命令”而不是“函数名称”
in addition to superjoe30's answer, you can use two levels of mapping (two dictionaries)
I think this would make it easier to allow user-defined mappings. i.e. so users can map their keys to "commands" rather than "the name of a function"
我现在所做的是拥有某种输入收集类/函数/线程,它将检查预定义的键->事件绑定的列表。
像这样的事情:
这基本上就是 superjoe30 正在做的事情,除了我不是直接调用回调,而是通过使用事件调度系统添加另一个级别的分离,以便任何关心按下的键的代码只需侦听该事件。
此外,键可以轻松绑定到不同的事件,这些事件可以从配置文件或其他内容中读取,并且任何未绑定到事件的键都会被忽略。
What I do nowadays is have some sort of input gathering class/function/thread which will check a list of predefined key->event bindings.
Something like this:
It's basically what superjoe30 is doing, except that instead of calling callbacks directly, I add another level of separation by using an event dispatching system, so that any code that cares about the keys being pressed simply listen for that event.
Also, keys can be easily bound to different events, which could be read from a config file or something and any key which is not bound to an event is simply ignored.