Moving the cursor in Java

前端 未结 2 775
借酒劲吻你
借酒劲吻你 2020-12-03 03:27

I want to make an app that measures the cursor\'s distance from the center of a component and then moves the cursor back to the center (like most PC video games do). Does an

相关标签:
2条回答
  • 2020-12-03 03:32

    Robot class can do the trick for you. Here is a sample code for moving the mouse cursor:

    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) {
    }
    
    0 讨论(0)
  • 2020-12-03 03:41

    Hi this will just be adding on. I use a Raspberry PI a lot so I've had to learn how to optimize my code this will be a lot shorter.

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

    don't forget to import:

    import java.awt.*;
    
    0 讨论(0)
提交回复
热议问题