在Java中移动光标
我想创建一个应用程序,测量光标距组件中心的距离,然后将光标移回中心(就像大多数PCvideo游戏一样)。 有没有人有什么建议?
机器人类可以为你做到这一点。 以下是移动鼠标光标的示例代码:
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) { }
嗨,这只是加入。 我经常使用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.*;