简体   繁体   English

如何检测Java 2d中是否按住了某个键?

[英]How do I detect if a key is held down in Java 2d?

I'm curious if I can tell if a key is being HELD, not pressed and let go. 我很好奇我能否说出某个键是否正在被帮助,未被按下并放开。

I need to know this because I'm going to play a shorter mp3 file if it is just pressed and a longer one if it is held. 我需要知道这一点,因为如果按下它,我将播放一个较短的mp3文件,如果按住它,则播放一个较长的mp3文件。

Thanks in advance! 提前致谢!

(I know I asked this for libgdx but I need to know for Java 2d now) (我知道我向libgdx询问了此信息,但现在我需要知道Java 2d)

The definition of "pressed" vs. "held" is up to you. “按下”与“保持”的定义由您决定。 Many programs even let you configure it. 许多程序甚至允许您对其进行配置。 You would have to start some sort of timer when a key was first pressed. 首次按下某个键时,您将必须启动某种计时器。 If the key is released before the timer completes, you cancel it because the key was only "pressed". 如果在计时器完成之前释放了键,则您将其取消是因为仅“按下”了键。 If the timer completes, the key was "held". 如果计时器完成,则该键被“按住”。 You can use java.util.Timer and java.util.TimerTask for this. 您可以为此使用java.util.Timerjava.util.TimerTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM