我写了一个“调整”功能,它做了肮脏的工作。
注意:如果必须调整超过30 px,则终止。
的<strong>
你可以打电话:
</强>
moveAndClick(1000,1000,TRUE);
private void moveAndClick(int x, int y, boolean click) throws AWTException {
robot.mouseMove(x, y);
adjustMouse(x, y);
if (click) {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
System.out.println("Move: " + x + " " + y + " Click:" + click);
}
private void adjustMouse(int x, int y) {
int realX = MouseInfo.getPointerInfo().getLocation().x;
int realY = MouseInfo.getPointerInfo().getLocation().y;
int aX = x;
int aY = y;
int count = 0;
while (realX != x || realY != y) {
System.out.println("Adjust:" + x + ":" + y + ">" + realX + ":" + realY + "");
aX = realX > x ? aX-- : aX++;
aY = realY > y ? aY-- : aY++;
robot.mouseMove(x, y);
try { // you can remove this part
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
realX = MouseInfo.getPointerInfo().getLocation().x;
realY = MouseInfo.getPointerInfo().getLocation().y;
if (count > 30) { // you can remove or increase this part
System.exit(0);
}
}
}
</code>