简体   繁体   中英

Java move mouse

I'm writing an application that will teach the basics of using a computer and I need to move the mouse to show what you have to do. I tried this:

public static void click(Point p) throws AWTException{
        Robot r = new Robot();
        r.mouseMove(p.x, p.y);
        r.mousePress(InputEvent.BUTTON1_MASK);
        try { Thread.sleep(100); } catch (Exception e) {}
        r.mouseRelease(InputEvent.BUTTON1_MASK);
}

Everything works, but the mouse teleports to the specified coordinates, without moving. So my question is: How effectively simulate the movement of the mouse, clicking the left and right and draging and dropping (as if you held the mouse button)

public static void click(Point p) throws AWTException{
    Robot r = new Robot();
    gradualMouseMove(p, r);
    r.mousePress(InputEvent.BUTTON1_MASK);
    try { Thread.sleep(100); } catch (Exception e) {}
    r.mouseRelease(InputEvent.BUTTON1_MASK);
}
    private static void gradualMouseMove(Point pointIn, Robot robot) throws InterruptedException {
    //TO ADJUST SPEED OF MOVEMENT
    //CHANGE:   1:sleep duration   2:deltaX #   3:deltaY #
    Point mouseLocation;        //Point for updating the currentLocation
    int deltaX;                 //X coordinate update value
    int deltaY;                 //Y coordinate update value
    boolean xInProgess = true;  //Stop condition for X
    boolean yInProgess = true;  //Stop condition for Y
    do{
        //update Mouse Location
        mouseLocation = MouseInfo.getPointerInfo().getLocation();
    //calculate X coordinates
        if(pointIn.getX() - mouseLocation.getX() > 2){  
            deltaX = 1;    //Mouse is LEFT of Destination
        }else if (pointIn.getX() - mouseLocation.getX() < -2){
            deltaX = -1;    //Mouse is RIGHT of Destination
        }else{
            xInProgess = false;     //X Done
            deltaX = 0;
        }
    //calculate Y coordinates
        if(pointIn.getY() - mouseLocation.getY() > 2){
            deltaY = 1;    //Mouse is ABOVE Destination
        }else if (pointIn.getY() - mouseLocation.getY() < -2){
            deltaY = -1;    //Mouse is BELOW Destination
        }else{
            yInProgess = false;    //Y Done
            deltaY = 0;
        }
        //move mouse on Screen by adjustment amount
        robot.mouseMove((int)(mouseLocation.getX()+deltaX), (int)(mouseLocation.getY()+deltaY));
        Thread.sleep(6);     //pause to slow down movement
    }while(xInProgess || yInProgess);   //loop until complete on X & Y
}

The above method gradualMouseMove is a simple version of "SLIDING" the mouse to a destination point.

*****Care that you do not use a point OFF your screen. This my prevent the loop from exiting if the mouse never gets there. You can add a count to end at a certain value if this is an issue.******

** Addition - You could add some math in there to have it go in a straight line to the destination if you like. (rather than the plateau currently there) **

This should give you a launch pad however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM