How to stop repeated keyPressed() / keyReleased() events in Swing

前端 未结 5 548
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 14:42

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 k

相关标签:
5条回答
  • 2020-11-28 14:52

    Here's the bug from Sun (includes sample work-arounds): http://bugs.sun.com/view_bug.do?bug_id=4153069

    0 讨论(0)
  • 2020-11-28 14:53

    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.

    0 讨论(0)
  • 2020-11-28 14:53

    You can find a pretty good workaround here.

    0 讨论(0)
  • 2020-11-28 14:55

    Use the KeyEventDispatcher implementation, with it you'll make key binds correctly.

    0 讨论(0)
  • 2020-11-28 15:10

    This question is duplicated here.

    Here's a hack implemented as an AWTEventListener that can be installed at the start of the application.

    0 讨论(0)
提交回复
热议问题