Question

I would like to drag and drop using the Robot class in Java. For some reason the code below isn't working. Is there an alternative to this method?

    public static void main (String args []){
    Robot robot = new Robot ();

    robot.mouseMove(350, 226);
    robot.keyPress(InputEvent.BUTTON1_MASK);
    robot.mouseMove(250, 350);
    robot.keyRelease(InputEvent.BUTTON1_MASK);

}

Was it helpful?

Solution

You need to use mousePress() and mouseRelease(), not keyPress() and keyRelease()

OTHER TIPS

This is helps anyone and you:

public static void click(int x , int y,int x2, int y2) throws AWTException, InterruptedException{
    Robot b11 = new Robot();

    b11.mouseMove(x, y);    
    b11.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    Thread.sleep(1000);//There is pause in miliseconds
    b11.mouseMove(x2, y2);
    b11.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top