Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the user is using the Trackpad and not the mouse in Java swing?

I hava a Java swing application that use a 'pan-able' JComponent to display large data. When the user move the mouse wheel, I listen to these events, and update the content of the JComponent from the scrolled amount.

I would like to have a different behavior depending on whether the user is

  • using a classical mouse
  • using a touchpad, as the ones that can be found on modern Mac laptops.

How could I detect that the user is using the mouse wheel vs the trackpad to generate the scroll event? I am relying in java 1.6 swing, si I cannot go to javaFX.

Story behind the question: I just wanted to add a cool inertia feel to the scrolling event when the user use the mouse wheel. But of course, on MacOSX, the trackpad has its own inertia stuff builtin. So I wanted to decide whether I should generate the inertial movement or not.

like image 386
Jean-Yves Avatar asked Jun 03 '15 08:06

Jean-Yves


2 Answers

Java Swing is an old technology, it supports the traditional mouse wheel rotation events.

When you use the old wheel mouse, or wheel track-pad, it will read the rotation of the hardware wheel.

When you use a modern laser mouse, the mouse movement will be translated to rotation motion.

When you use a touch track-pad like the one in modern Mac laptops, the scroll gesture will be translated into rotation motion, single & double touch as left and right click (based on OS mouse-pad configuration).

You can use libraries to check the input devices in detail, in case your mouse or track-pad is connected to your computer through USB, you can try J-USB library.

As for internal hardware, you first have to identify the type of OS, and based on that you can get information on system and hardware in Java.

Finally, if your application interacts with a user, I suggest asking the user what type of mouse they're using, and store that in configuration file or something.

like image 162
Khaled.K Avatar answered Oct 09 '22 05:10

Khaled.K


For MacOS you can get all the touchpad events in Java, I'd been searching myself for a while and just answered my own question. Leaving this here in case anyone else starts down the same rabbit hole

What replaces GestureUtilities in Java 9

like image 1
Hamish258 Avatar answered Oct 09 '22 05:10

Hamish258