1

I am building a Java game in Swing, and I have an issue where keyPressed events stop registering after holding the A or S keys for too long on Mac (this is a big problem considering that the game uses WASD movement).

After digging around Stack Overflow, I found out that holding down keys like this suspends focus because it attempts to open the accented characters menu, even though the normal list of accented characters does not appear. When I disable the character accents menu on my Mac, everything works fine. However, I'd like to know if there is a Java-native way to work around this behavior (such as a wrapper for KeyListeners) since I don't want all Mac users to have to deal with this issue.

William Merrill
  • 404
  • 4
  • 9
  • 1
    *"I'd like to know if there is a Java-native way to work around this behavior"* No. – Andrew Thompson Nov 21 '16 at 04:39
  • that's what I figured. – William Merrill Nov 21 '16 at 20:18
  • From an answer that should be a comment: *"Having the same issue and found a not so perfect workaround, although not sure whether it will apply to your application: https://stackoverflow.com/a/49783514/8371761"* – Frakcool Apr 11 '18 at 20:34
  • @AndrewThompson If not java-native, how about in a launcher script? `defaults write -g ApplePressAndHoldEnabled -bool false` will do it, but that's heavy-handed since it permanently affects every app on the system. – Don Hatch Jul 02 '18 at 03:54
  • Yes, there is a workaround for this within java. See https://github.com/brackeen/Scared/commit/17af314c5ebc8a6f6bc3215aed870a20bbb4ec48 (also https://github.com/TurboVNC/turbovnc/commit/1fcce19ae2930f02c3a222be2ab22d0a7fcc966c which is attributed to the previous link). I tried this in my app which suffered from the same problem, and it works. – Don Hatch Jul 02 '18 at 06:04

1 Answers1

1

I had the same question, and the issue was the Java version.

I had JDK 1.8.0_151, but apparently this issue was fixed in some iteration up to JDK 1.8.0_172.

You could fix the issue by updating your JDK for your development and prevent this issue for your users by checking their java version using org.apache.commons.lang3.SystemUtils

e.g.

if(isJavaVersionAtLeast(JavaVersion.JAVA_1_9)) {
    /* Insert message/pop-up to update and give link to Java download
     * You can exit the program using System.exit(0) or send them to the
     * download site or link using Desktop.getDesktop().browse(new URI(url))
     * Where url is the URL of the site or link in the form of a String
}
Gigi Bayte 2
  • 838
  • 1
  • 8
  • 20