Java trackpad 2-finger scroll listener

后端 未结 3 738
温柔的废话
温柔的废话 2020-12-19 07:12

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

相关标签:
3条回答
  • 2020-12-19 07:49

    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.

    0 讨论(0)
  • 2020-12-19 07:55

    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.

    0 讨论(0)
  • 2020-12-19 07:58

    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.

    0 讨论(0)
提交回复
热议问题