我正在做一个关于远程控制的项目,从客户端发送光标的内容x和y到服务器。
但
robot.mouseMove(x,y);
将仅将光标移动到特定点,而不会将光标从原始点移动
我找到了这个简单的算法来模拟鼠标的连续运动。
for (int i=0; i<100; i++){
int x = ((end_x * i)/100) + (start_x*(100-i)/100);
int y = ((end_y * i)/100) + (start_y*(100-i)/100);
robot.mouseMove(x,y);
}
但是这个算法仍然太简单了,它只是从一个点缓慢地移动到另一个点,这仍然不像人类的行为。
我读过一些关于网络远程控制的开源代码,我发现这个项目http://code.google.com/p/java-remote-control/使用了来自MouseListener类的方法调用MosueMovement,他们用它来执行“拖动”。
我想知道有没有人知道更好的方法?
发布于 2018-10-21 06:20:02
对于将来的任何人:我为Java开发了一个库,它模仿人类的鼠标移动。移动中的噪声/锯齿,正弦弧线,位置过冲等。另外,该库在编写时考虑了扩展和配置的可能性,因此,如果默认解决方案与情况不匹配,任何人都可以对其进行微调。现在可从Maven Central获得。
发布于 2012-08-26 23:59:01
看看我写的这个例子。您可以对此进行改进以模拟Joey所说的话。我写得非常快,有很多地方可以改进(算法和类设计)。请注意,我只处理从左到右的移动。
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
public class MouseMoving {
public static void main(String[] args) {
new MouseMoving().execute();
}
public void execute() {
new Thread( new MouseMoveThread( 100, 50, 50, 10 ) ).start();
}
private class MouseMoveThread implements Runnable {
private Robot robot;
private int startX;
private int startY;
private int currentX;
private int currentY;
private int xAmount;
private int yAmount;
private int xAmountPerIteration;
private int yAmountPerIteration;
private int numberOfIterations;
private long timeToSleep;
public MouseMoveThread( int xAmount, int yAmount,
int numberOfIterations, long timeToSleep ) {
this.xAmount = xAmount;
this.yAmount = yAmount;
this.numberOfIterations = numberOfIterations;
this.timeToSleep = timeToSleep;
try {
robot = new Robot();
Point startLocation = MouseInfo.getPointerInfo().getLocation();
startX = startLocation.x;
startY = startLocation.y;
} catch ( AWTException exc ) {
exc.printStackTrace();
}
}
@Override
public void run() {
currentX = startX;
currentY = startY;
xAmountPerIteration = xAmount / numberOfIterations;
yAmountPerIteration = yAmount / numberOfIterations;
while ( currentX < startX + xAmount &&
currentY < startY + yAmount ) {
currentX += xAmountPerIteration;
currentY += yAmountPerIteration;
robot.mouseMove( currentX, currentY );
try {
Thread.sleep( timeToSleep );
} catch ( InterruptedException exc ) {
exc.printStackTrace();
}
}
}
}
}
https://stackoverflow.com/questions/12131427
复制相似问题