Robot.mouseMove 没有正确移动到指定位置 [英] Robot.mouseMove not moving to specified location properly

查看:94
本文介绍了Robot.mouseMove 没有正确移动到指定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我为机器人运行 mouseMove 命令时,鼠标并不总是移动到相同的位置.例如,我有以下代码:

Whenever I run a mouseMove command for a robot, the mouse doesn't always go to the same location. For example, I have the following code:

import java.awt.Robot;
import java.util.concurrent.TimeUnit;

public class MainBot {
    public static void main(String[] args){
        try {
            Robot screenWin = new Robot();
            TimeUnit.SECONDS.sleep(2);
            screenWin.mouseMove(100, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码通常使鼠标在 X 处结束:

The code usually makes the mouse end up at the X:

首先,我点击运行(我正在使用 eclipse)并将我的鼠标移动到一个位置(在 2 秒计时器结束之前).然后 2 秒延迟结束,鼠标移动,然后脚本结束.问题是,鼠标似乎永远不会去同一个确切的地方两次.例如,鼠标应该移动到 (100, 300) 但它大部分时间看起来像 (0, 300) .然而,其他时候,如果我在开始时将鼠标移动到应该大致的位置,然后它会移动到正确的位置.

First, I hit run (I am using eclipse) and move my mouse to a location (before the 2 second timer is up). Then the 2 second delay finishes and the mouse moves and then the script ends. The problem is, the mouse never seems to go to the same exact place twice. For example, the mouse should go to (100, 300) but it goes to something that looks like (0, 300) most of the time. Other times, however, if I move the mouse at the beginning to where it should roughly be, then it goes to the right spot.

我知道鼠标应该在哪里使用画图来获取屏幕截图的像素位置,但我不认为这是因为位置不断变化.

I am getting where the mouse should be using Paint to get the pixel location of a screenshot but I don't think it is that because the location keeps changing.

有什么我遗漏了 mouseMove 的坐标是如何工作的吗?

Is there anything I'm missing how the coordinates for mouseMove work?

基本上,我用那个程序开始,然后我将鼠标移动到一个新位置(所以在 mouseMove 函数之前有一个不同的初始位置)然后 mouseMove 执行.每次我这样做时,鼠标都会移动到不同的位置.

Basically, I hit start with that program, then I move the mouse to a new position (so there is a different initial position before the mouseMove function) and then mouseMove executes. Each time I do this, the mouse goes to a different location.

推荐答案

OpenJDK 上存在一个未解决的错误,因此可能与此相关:

There's an open bug on OpenJDK, so this could be related:

https://bugs.openjdk.java.net/browse/JDK-8196030?jql=project%20in%20(JDK)%20AND%20component%20in%20(client-libs)%20AND%20Subcomponent%20in%20(java.awt)

Windows 10 Fall Creators 更新中可能引入的问题的错误详细信息,与屏幕缩放和 mouse_move 功能相关.

The bug details that a problem may have been introduced in Windows 10 Fall Creators update, related to screen scaling and a mouse_move function.

同时,您可以尝试将屏幕比例设置为 100% 而不是 125%,看看是否有帮助.

In the meantime, you could try to set your screen scale to 100% instead of 125% and see if it helps.

这篇关于Robot.mouseMove 没有正确移动到指定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆