【问题标题】:How to do smooth-scrolling by Java robot?如何通过 Java 机器人进行平滑滚动?
【发布时间】:2016-11-18 21:08:55
【问题描述】:

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

robot.mouseWheel(a); // int a

【问题讨论】:

  • 你试过this了吗?
  • @user6904265,不一样,滚动有效。例如,我想按像素滚动,而不是按大鼠标滚轮步骤。
  • @Антон 现在有办法绕过这些单位。这些槽口就是任何指点设备所提供的。其余的只是依赖于操作系统。如果您只想以较慢的速度滚动,那么实现起来非常简单。

标签: java mousewheel smooth-scrolling awtrobot


【解决方案1】:

卷轴的单位始终是“轮子的凹口”(在您问之前:这就是docs 中测量的命名方式)。这只是硬件实现的方式(更具体地说:鼠标)。每个“缺口”滚动多少像素只不过是操作系统配置。你不能用纯 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){}
        }
    }
}

【讨论】:

  • 是的,我的代码看起来已经像这个例子了......我认为有一些方法可以减少步骤。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多