Антон Антон - 21 days ago 5
Java Question

How to do smooth-scrolling by java robot?

I got used to my touch pad, that allows to scroll smoothly and very exactly, but I can not to simulate it by java robot - mousewheel is getting only integer parameters and a scrolling carried by steps. Can I simulate smoothly scrolling in java?

robot.mouseWheel(a); // int a

Answer

The unit of scrolls will always be by "notches of the wheel" (before you ask: that's how the measurement is named in the docs). This is simply how it's implemented by the hardware (to be more specific: the mouse). How many pixels are scrolled per "notch" is nothing but OS-configuration. You can't mess with that with pure java and I wouldn't recommend it, even if it was possible.

What you can do nevertheless is to slow down the speed at which the robot scrolls:

import java.awt.Robot;

public class Test{
    public static void main(String[] args)
        throws Exception
    {
        //time to switch to a specific window where the robot ought to be tested
        try{ Thread.sleep(2000); }catch(InterruptedException e){}

        Robot r = new Robot();
        for(int i = 0; i < 20; i++){
            //scroll and wait a bit to give the impression of smooth scrolling
            r.mouseWheel(1);
            try{ Thread.sleep(50); }catch(InterruptedException e){}
        }
    }
}