gpt4 book ai didi

Java机器人类——模拟鼠标点击而不移动光标

转载 作者:行者123 更新时间:2023-11-30 07:41:01 25 4
gpt4 key购买 nike

我正在尝试使用 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());
}
}
}

}

最佳答案

只需将 Thread.sleep(3000) 放在 try{ } block 的末尾即可。

您当前的代码在将鼠标移动到旧的原始位置后立即获取新的“原始位置”

没有循环

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());
}
}

关于Java机器人类——模拟鼠标点击而不移动光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34699042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com