我试图模拟真正的鼠标点击而不用javas机器人类移动光标。是否可以将此代码放入while循环或其他东西来注册鼠标位置,并在实际点击后将鼠标移动到该位置?到目前为止,代码被告知将鼠标移动到注册的鼠标位置(它在我运行代码时注册)但是我希望它将鼠标移动到与我的鼠标打开时相同的位置,而不是在角落的某个位置。谢谢。
while(true) {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int xOrig = (int)b.getX();
int yOrig = (int)b.getY();
try {
Robot r = new Robot();
Thread.sleep(3000);
r.mouseMove(720, 360);
r.mousePress(InputEvent.BUTTON1_MASK); //press the left mouse button
r.mouseRelease(InputEvent.BUTTON1_MASK); //release the left mouse button
//move the mouse back to the original position
r.mouseMove(xOrig, yOrig);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
}
答案 0 :(得分:1)
只需将Thread.sleep(3000)
放在try {}块的末尾。
您当前的代码获取新的"原始位置"在它将鼠标移动到旧的原始位置之后
没有循环
public static void main(String[] args) throws InterruptedException {
//For testing
Thread.sleep(1000);
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int xOrig = (int) b.getX();
int yOrig = (int) b.getY();
try {
Robot r = new Robot();
r.mouseMove(720, 360);
// press the left mouse button
r.mousePress(InputEvent.BUTTON1_MASK);
// release the left mouse button
r.mouseRelease(InputEvent.BUTTON1_MASK);
// move the mouse back to the original position
r.mouseMove(xOrig, yOrig);
Thread.sleep(3000);
} catch (Exception e) {
System.out.println(e.toString());
}
}