Drag/Drop Robot Class
Asked Answered
P

2

6

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

}

Pape answered 20/2, 2012 at 17:31 Comment(2)
How far apart are those two x,y points? - if they are too close together, the system may not register a drag event.Asexual
The new position is approximately 25px away. Sorry I included some irrelevant objects. I have now edited the code.Pape
A
3

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

Asexual answered 24/2, 2012 at 20:55 Comment(1)
Wow. I cannot believe I overlooked that. What a stupid mistake. Thank you for your help.Pape
H
0

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

}
Halicarnassus answered 17/2, 2019 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.