AWT Robot not able to drag a window

泄露秘密 提交于 2019-12-11 01:22:45

问题


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.


回答1:


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



回答2:


I had a similar problem. Try adding Thread.sleep(100); (or more then 100) between each Robot operation.




回答3:


Why it didn't work for you with Sikuli? What was a problem? I tried it by myself and it worked. My environment: Win7 with disabled window transparency, Sikuli r930. To simplify a test, some window is already opened before starting a test. This is my working example:

winExpl_topRightCornerImg = Pattern("1368690528515.png").targetOffset(-2,-13) 
"""winExpl_topRightCornerImg is a part of a window containing top bar.
I captured huge rectangle containing navigation arrows and I changed target offset,
so that Sikuli clicks only on a bar"""
anyRectangleAtDesktop = "1368690615156.png" #any particular part of a desktop
drag(winExpl_topRightCornerImg)
dropAt(anyRectangleAtDesktop)



回答4:


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.



来源:https://stackoverflow.com/questions/16466461/awt-robot-not-able-to-drag-a-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!