如何通过Java机器人进行平滑滚动?
问题描述:
我已经习惯了我的触摸板,它允许平滑滚动,非常精确,但我无法通过Java机器人模拟它 - mousewheel只获取整数参数,并且通过步骤进行滚动。我可以模拟在Java平滑滚动吗?如何通过Java机器人进行平滑滚动?
robot.mouseWheel(a); // int a
答
滚动的单位将始终以“轮的凹槽”(你问之前:这是测量是如何在docs命名)。这仅仅是硬件实现的方式(更具体一些:鼠标)。每个“缺口”滚动的像素数是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){}
}
}
}
+0
是的,我的代码看起来像这个例子已经...我认为有一些方法来减少步骤。 –
你已经尝试过[这](http://stackoverflow.com/questions/15714609/robot-class -mousewheel - 不工作)? – user6904265
@ user6904265,这是不一样的,滚动的作品。 我想通过px滚动例如,而不是通过大鼠标滚轮步骤。 –
@Антонtheres现在的方式来解决单位。这些缺口就是任何指示设备提供的。其余的只是依赖于操作系统。如果您只想以较慢的速度滚动,则实现起来非常简单。 – Paul