我想制作一个应用程序,可以测量光标距离组件中心的距离,然后将光标移回到中心(就像大多数PC视频游戏一样)。有没有人有什么建议?
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) {
}
嗨,这只是一个补充。我经常使用树莓派,所以我必须学会如何优化我的代码,这篇文章会更加简短。
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.*;