拖放机器人类

发布于 2025-01-07 13:26:18 字数 330 浏览 3 评论 0原文

我想使用 Java 中的 Robot 类进行拖放。由于某种原因,下面的代码不起作用。这种方法有替代方法吗?

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2025-01-14 13:26:18

您需要使用 mousePress()mouseRelease(),而不是 keyPress()keyRelease()

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

╰◇生如夏花灿烂 2025-01-14 13:26:18

这对任何人和你都有帮助:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文