Java 机器人如何平滑滚动?

How to do smooth-scrolling by Java robot?

我已经习惯了我的触摸板,它允许平滑且非常精确地滚动,但我无法通过 Java 机器人模拟它 - 鼠标滚轮仅获取整数参数和按步进滚动。我可以在 Java 中模拟平滑滚动吗?

robot.mouseWheel(a); // int a

卷轴的单位总是 "notches of the wheel"(在你问之前:这就是 docs 中度量的命名方式)。这就是硬件(更具体地说:鼠标)的实现方式。每个 "notch" 滚动多少像素只是 OS 配置。你不能用纯 java 搞砸它,我不会推荐它,即使它可能。

不过你能做的就是减慢机器人滚动的速度:

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){}
        }
    }
}