键盘轮询系统
我正在为 Java 游戏寻找高效的键盘输入系统。 目前我正在使用在这里找到的一些代码:
似乎效率有点低(请评论您对代码的想法)。 完整的代码可以在这里找到:
我才刚刚开始寻找进入一个新的解决方案并将代码分开,但是有人对在主游戏循环中执行此操作的有效方法有任何建议(包括解释)吗? 任何帮助,将不胜感激。
I'm looking for an efficient keyboard input system for a game in Java. At present I'm using some code I found here:
it seems a little inefficient however (please comment as to your thoughts on the code). The full code is available here:
I've only just started looking into a new solution and pulling the code apart, but does anybody have any suggestions for an efficient way of doing this in a main game loop (including an explanation)? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置一个KeyEventListener; 调用时,将当前密钥放入一键缓冲区中。
在计算线程或轮询线程中,定期查询当前密钥缓冲区。 现在,您不再需要不断地查看 I/O,而是让事件侦听器执行此操作(该侦听器已经经过大量优化并干净地实现以使用底层硬件),同时您还可以继续进行定期轮询。
Set up a KeyEventListener; when invoked, put the current key into a one-key buffer.
In the computation thread, or in a polling thread, query the current key buffer periodically. Now, instead of that constant peeking at the I/O, you have the event listener doing it (which is already heavily optimized and cleanly implemented to use the underlying hardware) while you keep having your periodic polling.
我认为这是很棒的代码。 链接的作者实现了从键盘拉式数据传输,而不是通常的 KeyListener 推送方法。 此实现支持 N 键翻转,但是,限制USB同时支持6-8个按键。
如果你想清理一下它,那么只存储你想要注意的键。 然而,这不会为你节省太多,每次按下一个键时,只会将一个 int 写入数组。
您到底发现什么效率低下? 首先,我在链接的代码中没有看到任何循环。
I think it's great code. The linked author has implemented a pull style data transfer from the keyboard instead of the usual KeyListener push method. This implementation supports N-key rollover, however, the limit with USB is 6-8 keys simultaneously.
If you want to clean it up a bit then only store the keys that you want to pay attention to. However, this will not save you much, only a single int is written to an array each time a key is pressed.
What exactly do you find inefficient? For starters, I don't see any loops in the linked code.