InputMap - register a keystroke that monitors key press, and no virtual repeats

0__

Apparently this question has been asked before and before and before, but either the answers are not solving the problem or use a language not understandable by me.

I have to register a keyboard action using JComponent.WHEN_IN_FOCUSED_WINDOW, so a KeyListener is not an alternative. I have to track pressing down and releasing the key, say a cursor key. I cannot have multiple press/release firings due to some built-in key repeat. I really want to know when the key is physically pressed and depressed. E.g., start a timer when pressed, stop the timer when released (and do not start and stop the timer dozens of times because of the virtual key repeats).

I believe this worked correctly on OS X, but I am on Linux now (OpenJDK 7).

How?

0__

As multiple people have noted, this is an oddity (or: bug) on Linux.

The work-around goes as follows. Let's say the press action class is called ActionPress, and the release action class is called ActionRelease.

  • both maintain a long lastWhen that is updated in actionPerformed with e.getWhen. They have a public method getLastWhen that can be called by their counterparts.
  • ActionRelease maintains a javax.swing.Timer with a few milliseconds. It's regular actionPerformed restarts the timer, nothing else.
  • ActionPress in its actionPerformed calls the corresponding ActionRelease's getLastWhen. If the result is identical to its own lastWhen, ignore the action and return, otherwise perform the action.
  • ActionRelease in the additional ActionListener for the Timer performs the actual action. In its getLastWhen implementation, it stops the timer before return lastWhen.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to Convert A Tab Key Press Into An Enter Key Press For Web Pages?

来自分类Dev

多个键的InputMap

来自分类Dev

GtkButton发出GDK_KEY_PRESS

来自分类Dev

Matplotlib'key_press_event'不响应

来自分类Dev

Vim exit Insert mode with some key press

来自分类Dev

on_key_press()事件阻止on_mouse_press()和on_mouse_motion()事件持续1秒

来自分类Dev

PyAutoGUI Key Press在Linux Ubuntu中性能降低

来自分类Dev

JavaFX InputMap/ActionMap 等价物?

来自分类Dev

socketChannel.register()和key.interestOps()之间的区别?

来自分类Dev

如何将参数传递给fig.canvas.mpl_connect('key_press_event',on_key)中的on_key?

来自分类Dev

在何处以及如何实例化inputmap和actionmap

来自分类Dev

使用key_press_event为不同类型的pick_event选择一个“模式”

来自分类Dev

使用key_press_event为不同类型的pick_event选择一个“模式”

来自分类Dev

如何声明KeyStroke-按下并释放?

来自分类Dev

如何统一启用混乱的-keystroke选项?

来自分类Dev

Square Retrofit repeats network calls

来自分类Dev

InputMap-注册一个监视按键的击键,并且没有虚拟重复

来自分类Dev

如何在VB.Net中的Virtual-Key语句中使用变量

来自分类Dev

monitors.xml和LightDM

来自分类Dev

Java KeyStroke.getKeyStroke修改器已更改

来自分类Dev

Java KeyStroke.getKeyStroke修改器已更改

来自分类Dev

这些运营商在做什么?字符串键 = 偏移 == -1 ? keyStroke : keyStroke.substring( offset + 1 );

来自分类Dev

“对成员'scheduledTimerWithTimeInterval(_:invocation:repeats :)的模糊引用”

来自分类Dev

How to use dual monitors on a system with 2 computers?

来自分类Dev

Gnome 3.26不尊重monitors.xml

来自分类Dev

'babel-core / register'与'babel-register'

来自分类Dev

ActiveAdmin,register_page和register

来自分类Dev

__fastcall vs register语法?

来自分类Dev

OR instruction in assembly into ECX register

Related 相关文章

  1. 1

    How to Convert A Tab Key Press Into An Enter Key Press For Web Pages?

  2. 2

    多个键的InputMap

  3. 3

    GtkButton发出GDK_KEY_PRESS

  4. 4

    Matplotlib'key_press_event'不响应

  5. 5

    Vim exit Insert mode with some key press

  6. 6

    on_key_press()事件阻止on_mouse_press()和on_mouse_motion()事件持续1秒

  7. 7

    PyAutoGUI Key Press在Linux Ubuntu中性能降低

  8. 8

    JavaFX InputMap/ActionMap 等价物?

  9. 9

    socketChannel.register()和key.interestOps()之间的区别?

  10. 10

    如何将参数传递给fig.canvas.mpl_connect('key_press_event',on_key)中的on_key?

  11. 11

    在何处以及如何实例化inputmap和actionmap

  12. 12

    使用key_press_event为不同类型的pick_event选择一个“模式”

  13. 13

    使用key_press_event为不同类型的pick_event选择一个“模式”

  14. 14

    如何声明KeyStroke-按下并释放?

  15. 15

    如何统一启用混乱的-keystroke选项?

  16. 16

    Square Retrofit repeats network calls

  17. 17

    InputMap-注册一个监视按键的击键,并且没有虚拟重复

  18. 18

    如何在VB.Net中的Virtual-Key语句中使用变量

  19. 19

    monitors.xml和LightDM

  20. 20

    Java KeyStroke.getKeyStroke修改器已更改

  21. 21

    Java KeyStroke.getKeyStroke修改器已更改

  22. 22

    这些运营商在做什么?字符串键 = 偏移 == -1 ? keyStroke : keyStroke.substring( offset + 1 );

  23. 23

    “对成员'scheduledTimerWithTimeInterval(_:invocation:repeats :)的模糊引用”

  24. 24

    How to use dual monitors on a system with 2 computers?

  25. 25

    Gnome 3.26不尊重monitors.xml

  26. 26

    'babel-core / register'与'babel-register'

  27. 27

    ActiveAdmin,register_page和register

  28. 28

    __fastcall vs register语法?

  29. 29

    OR instruction in assembly into ECX register

热门标签

归档