AWT Robot 无法拖动窗口 [英] AWT Robot not able to drag a window
问题描述
我正在尝试使用 AWT 机器人移动 Windows 资源管理器窗口.机器人运行Java 7,操作系统为Windows 7.
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.
我开始使用 Sikuli:
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.
推荐答案
在 sikuli 中使用 mouse.drag() 然后使用 mouse.drop().示例:
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());
对于 Java 机器人 API:您应该按顺序调用 mouseMove()、mousePress()、mouseMove() 和 mouseRelease().示例:
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);
这篇关于AWT Robot 无法拖动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!