On mac, in Java, keyPressed event doesn't fire for certain keys

后端 未结 4 1767
南旧
南旧 2021-01-14 19:01

This is a really weird issue, but I just have a simple keyListener added to a JPanel that prints on keyPressed and on keyReleased. Usually it works fine, but on certain keys

相关标签:
4条回答
  • 2021-01-14 19:41

    I ran into this issue recently, it is caused by the MacOS showing a context menu when you hold certain keys down (To allow you to chose alternative language characters) and the bug report page had a good solution that worked for me:

    https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8167263

    To disabled the character accent menu and enable auto-repeat, type the following at the command prompt:

    defaults write -g ApplePressAndHoldEnabled -bool false
    

    This can be reversed with the following:

    defaults write -g ApplePressAndHoldEnabled -bool true
    

    I just tried this on the mac console and my java application no longer has the key problem.

    0 讨论(0)
  • 2021-01-14 19:44

    More troublesome keys were reported here

    https://community.oracle.com/thread/4115318

    It seems at this moment the best workaround is to avoid the use of

    z, c, n, a, s, e, y, u, i, o

    0 讨论(0)
  • 2021-01-14 19:50

    I fixed it by going into my system preferences -> keyboard and moving the key repeat slider all the way to the left to "Off".

    0 讨论(0)
  • 2021-01-14 19:57

    It appears that this is a Java bug that arose after people upgraded to MacOS Sierra:

    • Java Bug Database

    • Reddit post

    As a temporary workaround you might try using keys other than the standard WASD for controlling movement.

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