如何处理Java中的同时按键? [英] How do I handle simultaneous key presses in Java?

查看:131
本文介绍了如何处理Java中的同时按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何处理Java中的同时按键?



我正在尝试写一个游戏,需要一次处理多个按键。



当我拿着一个键(我们说要向前移动),然后我按住另一个键(例如左转),检测到新的键,而按下旧的键没有被检测到。

解决方案

一种方法是跟踪自己当前正在关闭的键。



当您获得一个keyPressed事件时,将新的密钥添加到列表中;当您获得一个keyReleased事件时,从列表中删除该密钥。



然后在游戏循环中,您可以根据密钥列表中的内容执行操作。 p>

How do I handle simultaneous key presses in Java?

I'm trying to write a game and need to handle multiple keys presses at once.

When I'm holding a key (let's say to move forward) and then I hold another key (for example, to turn left), the new key is detected but the old pressed key isn't being detected anymore.

解决方案

One way would be to keep track yourself of what keys are currently down.

When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.

Then in your game loop, you can do actions based on what's in the list of keys.

这篇关于如何处理Java中的同时按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆