Moving the cursor in Java

前端 未结 2 883
借酒劲吻你
借酒劲吻你 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: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.*;
    

提交回复
热议问题