代码之家  ›  专栏  ›  技术社区  ›  Ky -

在Java中移动光标

  •  20
  • Ky -  · 技术社区  · 14 年前

    我想制作一个应用程序,测量光标与组件中心的距离,然后将光标移回中心(就像大多数PC视频游戏一样)。有人有什么建议吗?

    2 回复  |  直到 14 年前
        1
  •  36
  •   Faisal Feroz    14 年前

    机器人类可以为你做这个把戏。下面是移动鼠标光标的示例代码:

    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) {
    }
    
        2
  •  4
  •   Rory McCrossan Hsm Sharique Hasan    10 年前

    嗨,这只是个补充。我经常使用树莓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.*;