4

Trying to migrate large Swing app to Java 9, most of the macOS specific features are now implemented in the Desktop() class, or happen as they should, i.e. com.apple.eawt.FullScreenUtilities isn't needed as windows use macOS full screen as they should.

I've not been able to find a replacement for GestureUtilities, which allows registration of GestureListeners for trackpad gestures, such as swipe, magnify. https://bugs.openjdk.java.net/browse/JDK-8057788 claims it's fixed in Java 9, however the class is now inaccessible.

I've been unable to find any reference as to how to make this work on Java 9, other than migrating to JavaFX which would be a major effort. Anyone know how to get events for trackpad gestures in Java 9 please?

Hamish258
  • 305
  • 2
  • 9

1 Answers1

2

Here I am 4 years later answering my own question, so I can reference it in all the other stack overflow posts asking similar questions that languish unanswered.

Short answer is

  1. Ensure you have at least jdk 17.02+b2 (I tested with 17.02+b8)

  2. Add -XDignore.symbol.file --add-exports java.desktop/com.apple.eawt.event=ALL-UNNAMED to your javac invocation

  3. Add --add-opens java.desktop/com.apple.eawt.event=ALL-UNNAMED to your invocation of java that runs your app

You see, GestureUtilities never really went away, they got hidden away, and endured a few bugs on the way. Props to Martijn for the comment in his bespoke answer to the problem https://github.com/mcourteaux/MultiTouch-Gestures-Java, that sent me down the rabbit hole to try and make it all work.

https://gist.github.com/alanwhite/95df3ea87b048149fb99d2b10212f11b contains working code.

Hamish258
  • 305
  • 2
  • 9
  • 1
    Why JDK 17.0.2 is needed, I can see the same class on OpenJDK 11.0.14 ? – bric3 Feb 06 '22 at 16:28
  • 1
    I was working with jdk17 and no eawt events were flowing but there were native exceptions being spewed on stderr when using the touchpad. https://bugs.openjdk.java.net/browse/JDK-8275131 appears to be the reason, and when I upgraded from 17.0 to 17.02+b8 it all started working. If it all worked fine at versions of 11 that's great news. More than anything I wanted to start getting answers out as there's so many SO posts, unanswered, posing similar questions. – Hamish258 Feb 07 '22 at 09:24
  • The codebase I am currently working on is targeting JDK 11. Currently I haven't been able to make it work with a JDK 11 but I didn't tried hard enough yet. – bric3 Feb 07 '22 at 09:32
  • 1
    Wrote up a blog post on my findings in case useful https://medium.com/@alan.r.white/trackpad-gestures-on-macos-in-java-1fdef37f3d77 – Hamish258 Feb 07 '22 at 14:11
  • 1
    And updated gist so it compiles on platforms other than mac, but of course won't work as they don't have the Apple APIs https://gist.github.com/alanwhite/42502f20390baf879d093691ebb72066 – Hamish258 Jun 02 '22 at 20:38