Moving the cursor in Java

前端 未结 2 881
借酒劲吻你
借酒劲吻你 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) {
    }
    

提交回复
热议问题