8

how can i detect a 2-finger scroll on a laptop trackpad in java? I've been searching google and here but can't find anything on scrolling using a trackpad let alone how to listen for it. any help would be greatly appreciated. thanks.

Joe
  • 1,326
  • 7
  • 34
  • 52
  • The two finger scroll will be hardware dependent won't it? – Kevin D Nov 24 '11 at 12:28
  • 3
    Do you have to distinguish between a scroll from a mouse and a trackpad scroll? – Roger Lindsjö Nov 24 '11 at 12:30
  • @KevinD - it's possible, i was just wondering that since java is platform independent there might be a solution – Joe Nov 24 '11 at 15:34
  • @RogerLindsjö - i am looking to implement a listener that responds to a 2-finger scroll on a trackpad, so i guess the answer to your question is yes but i don't need to implement a mouse scroll – Joe Nov 24 '11 at 15:36
  • @Joe For Java and I think most of other applications, a mouse wheel scroll and a 2-finger gesture on the trackpad ARE the same thing. If you want to implement a 2-finger trackpad scroll that DOES NOT work with the mouse wheel, you will have to be able to know that the scroll originates from the trackpad and that may be impossible using Java. – Vincent Robert Nov 24 '11 at 17:03
  • @VincentRobert - okay i suspected that but wasn't sure – Joe Nov 24 '11 at 17:36

3 Answers3

8

I made this sample program

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;

public class ScrollTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200,200);
        frame.addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent event) {
                if (event.isShiftDown()) {
                    System.err.println("Horizontal " + event.getWheelRotation());
                } else {
                    System.err.println("Vertical " + event.getWheelRotation());                    
                }
            }
        });
        frame.setVisible(true);
    }
}

It will print if the scroll is horizontal or vertical and how much the scroll was when you scroll within the opened window on a mac with a touchpad.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
4

If this is about listening for user scrolls, you can do it by adding a MouseWheelListener to your control. See How to Write a Mouse-Wheel Listener for more information.

If this is about detecting specific events from the trackpad and not the mouse, I don't known of any Java feature to implement this.

Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
  • this is kinda both, i want to listen for user scrolls that originate from a trackpad, the links you gave will come in handy but it doesn't work for what i need (i'm on a mac if that makes any difference). – Joe Nov 24 '11 at 15:39
  • I was googling for any issue between MouseWheelListener and Mac OS and the only thing I could find was a confirmation that MouseWheelListener was the way to go: http://lists.apple.com/archives/java-dev/2008/Jun/msg00169.html Sorry if that does not work for you, I will try on my own Mac if I can find the time at home. – Vincent Robert Nov 24 '11 at 16:59
  • okay... perhaps it is the IDE i am using can't, won't or doesn't register scrolls. i'm using Processing and all the mouse listeners work except for MouseWheelListener. i'll check on their forum for anything. thanks for your answer. – Joe Nov 24 '11 at 17:35
  • i guess i need to use the method detailed here to use a mouse wheel in processing: [Processing Wiki](http://wiki.processing.org/w/Wheel_mouse) – Joe Nov 24 '11 at 18:42
0

Finally an answer that will listen to native scrolling. Take a look at my question and answer here: https://stackoverflow.com/a/31190973/155137

The project also detects scroll gestures and reports them nicely. The scrolling is as smooth as a native cocoa application.

Community
  • 1
  • 1
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287