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

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

问题描述

所以我遇到的问题似乎是只在 Linux 上发生的错误.我试图在按下某个键时记录我的 Swing 应用程序,然后检测该键何时被释放.现在这不应该成为问题,因为 KeyListener 应该为我处理这个.

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.

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

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?

谢谢.

推荐答案

所以我遇到的问题似乎是一个只发生的错误在 Linux 上

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

是的,这是一个 Linux 问题.

Yes this is a Linux problem.

在 Windows 上,当一个键被按下时,您会收到多个 KeyPressed 事件,但只有一个 KeyReleased 事件.

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.

但我认为解决方案的基础是使用计时器.当按下该键时,您将启动计时器.当您按下另一个键时,您将重新启动计时器.所以只要 Timer 间隔大于键盘的重复率,当一个键被按住时,Timer 将不断重置.当 keyPresses 停止生成时,计时器将触发,因此您假设该键已被释放.这意味着您将延迟处理 keyReleased.

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天全站免登陆