我想做一个应用程序,可以测量光标与组件中心的距离,然后将光标移回组件中心(就像大多数PC视频游戏一样)。有人有什么建议吗?
发布于 2010-11-20 15:19:45
Robot类可以帮你完成这项工作。以下是移动鼠标光标的示例代码:
try {
// These coordinates are screen coordinates
int xCoord = 500;
int yCoord = 500;
// Move the cursor
Robot robot = new Robot();
robot.mouseMove(xCoord, yCoord);
} catch (AWTException e) {
}
发布于 2015-09-18 23:38:29
嗨,这将只是添加。我经常使用Raspberry PI,所以我不得不学习如何优化我的代码,这将会短得多。
try {
//moves mouse to the middle of the screen
new Robot().mouseMove((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
//remember to use try-catch block (always, and remember to delete this)
} catch (AWTException e) {
e.printStackTrace();
}
别忘了导入:
import java.awt.*;
https://stackoverflow.com/questions/4231458
复制相似问题