Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWT Robot not able to drag a window

I'm trying to move a Windows Explorer window using the AWT Robot. The robot is running in Java 7, and the OS is Windows 7.

I'm able to move the mouse and click on things, but when I try to click-and-drag, it doesn't seem to be pressing the button at all. I can't see what's wrong, or think of how to figure out what's happening.

I started out using Sikuli:

mouse.mouseDown(InputEvent.BUTTON1_MASK);
mouse.drop(targetLocation);

When that didn't work, I tried a lower-level implementation, working with the robot directly:

Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(targetLocation.getX(), targetLocation.getY());
robot.mouseRelease(InputEvent.BUTTON1_MASK);

The mouse starts in the correct place and moves to the correct destination, but doesn't seem to press the button.

like image 472
Nathaniel Waisbrot Avatar asked Mar 25 '23 00:03

Nathaniel Waisbrot


2 Answers

In sikuli use mouse.drag() then mouse.drop(). Example:

ScreenRegion fullScreenRegion=new ScreenRegion();
ImageTarget dragImageTarget=new ImageTarget("dragTargetFile");
ScreenRegion dragTargetRegion=fullScreenRegion.find(dragImageTarget);
ImageTarget dropImageTarget=new ImageTarget("dropTargetFile");
ScreenRegion dropTargetRegion=fullScreenRegion.find(dropImageTarget);

Mouse mouse = new DesktopMouse();
mouse.drag(dragTargetRegion.getCenter());
mouse.drop(dropTargetRegion.getCenter());

For the Java Robot API: You should call mouseMove(), mousePress(), mouseMove(), and then mouseRelease() in that order. Example:

Robot robot=new Robot();
// drag
robot.mouseMove(x1, y1);
robot.mousePress(InputEvent.BUTTON1_MASK);
// drop
robot.mouseMove(x2, y2);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
like image 187
kalharbi Avatar answered Mar 26 '23 13:03

kalharbi


I had the exactly the same problem. Even tried what @Andrzej Kasp said, add thread sleep for certain mount of time. It does not work, until I made some modification based on @Andrzej Kasp `s comments. Thanks to Andrzej Kasp.

The whole reason why it does not work is because the thread got executed in series. I tried to add thread.sleep(100); between each action does not work. Codes as below:

robot.mouseMove(x1,y1);                      //step 1
thread.sleep(100);
robot.mousePress(InputEvent.BUTTON1_MASK);   //step 2
thread.sleep(100);
robot.mouseMove(x2, y2);                     //step 3
thread.sleep(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK); //step 4

It does not work for me. It might work for you though. Depends on the computer. The whole theory behind this is that, if you want to make sure if work, you have to make sure the mouse is pressed while the cursor is moving. ie. step 2 and step 3 are being executed at the same time. In normal case, computer will not create a new thread while running this program unless you make use of thread.sleep(), it will suspend the current thread and processor time will be assigned to other threads. So you do not have to add thread.sleep(100) after each step, instead, you only need to add thread.sleep(100) between step 2 and step 3. I have tested these codes and they worked. Codes as below:

robot.mouseMove(x1,y1);                      //step 1
robot.mousePress(InputEvent.BUTTON1_MASK);   //step 2
thread.sleep(100);
robot.mouseMove(x2, y2);                     //step 3
robot.mouseRelease(InputEvent.BUTTON1_MASK); //step 4

Let me know if you have more questions.

like image 26
Ivan Ouyang Avatar answered Mar 26 '23 13:03

Ivan Ouyang