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

后端 未结 4 1795
南旧
南旧 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.

提交回复
热议问题