如何在Swing中停止重复的keyPressed()/ keyReleased()事件 [英] How to stop repeated keyPressed() / keyReleased() events in Swing

查看:140
本文介绍了如何在Swing中停止重复的keyPressed()/ keyReleased()事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到的问题似乎是一个仅在Linux上发生的错误。当我按下一个按键的时候,我试图摆放我的秋千应用程序记录,然后检测该键是否被释放。现在不应该是有问题的,因为 KeyListener 应该为我处理这个问题。



问题是当我按下键时,我得到很多重复的keyPressed( )/ keyReleased()事件,而不是单个keypressed()。有没有人有解决方案或解决方法知道什么时候真的在linux上发布密钥?



谢谢。

解决方案


所以我有
的问题似乎是在Linux上只有
的错误


是的,这是一个Linux问题。



在Windows上按下一个键时,您可以获得多个KeyPressed事件,但只有一个KeyReleased事件。



这个问题经常被询问,我从来没有见过一个很好的解决方案。



但我认为解决方案的基础是使用Timer。按下该键后,启动定时器。当你得到另一个keyPressed你重新启动计时器。因此,只要定时器间隔大于键盘的重复速度,按住一个键时,定时器将持续复位。当keyPresss停止生成时,定时器将触发,以便您假定该键已被释放。这意味着您将处理keyReleased的延迟。


So the problem that I am having appears to be a bug that occurs only on Linux. I'm trying to have my swing app record when a key is pressed down, then to detect when that key is released. Now that shouldn't be in issue because KeyListener is supposed to handle this for me.

The problem is that when I hold the key down I get lots of repeated keyPressed()/keyReleased() events instead of just the single keypressed(). Does anyone have a solution or workaround for knowing when a key is really released on linux?

Thank you.

解决方案

So the problem that I am having appears to be a bug that occurs only on Linux

Yes this is a Linux problem.

On Windows when a key is held down you get multiple KeyPressed events but only a single KeyReleased event.

This question is asked often and I've never seen a good solution.

But I think the basis of a solution is to use a Timer. When the key is pressed you start a Timer. When you get another keyPressed you restart the Timer. So as long as the Timer interval is greater than the repeat rate of the key board the Timer will be continually reset when a key is held down. When keyPresses stop being generated the Timer will fire so you assume the key has been released. This implies you will have a delay in processing the keyReleased.

这篇关于如何在Swing中停止重复的keyPressed()/ keyReleased()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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