8

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

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

Mouse Location after running 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.

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

Edit: 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.

luator
  • 4,769
  • 3
  • 30
  • 51
DarkHorse
  • 963
  • 1
  • 10
  • 28
  • there is a lot of interaction you are describing - I dont know what you are doing - maybe there is a program that you can show – gpasch Feb 15 '18 at 03:13
  • The only other part of the program is the `main` it is inside. I added a little clarification. – DarkHorse Feb 15 '18 at 03:43
  • I dont know what is wrong - thats why I'm asking a complete program - if a hidden detail is found dont blame me - I use the command and it goes to the exact location – gpasch Feb 15 '18 at 03:49
  • @gpasch posted the code and the location of the mouse after running. – DarkHorse Feb 15 '18 at 04:09
  • oh ok I think 100 is from the beginning of the screen you are showing your work screen? if what you are showing is your work window which is 100 pixels from the left of the screen then its right - if what showing is the whole monitor then I dont know what is wrong – gpasch Feb 15 '18 at 04:26
  • @gpasch that is a screenshot of my whole monitor. – DarkHorse Feb 15 '18 at 05:09
  • 1
    @Shiven, [other people seem to have this same issue](https://stackoverflow.com/questions/48837741/java-robot-mousemovex-y-not-producing-correct-results), and there seems to be [an open ticket against the JDK](https://bugs.openjdk.java.net/browse/JDK-8186063) whose description may match your issue as well. – Matt Clark Feb 17 '18 at 07:39
  • Since this appears to work all the time on some systems, @Shiven please add details about what exact JDK you are using with JDK version, Windows version etc – Deepak Feb 20 '18 at 15:01

5 Answers5

7

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)

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.

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

Syl
  • 2,733
  • 2
  • 17
  • 20
  • 1
    Why is this answer downvoted? It's exactly why this is happening. You can verify this on any Windows 10 with the creators update. – mjuopperi Feb 27 '18 at 10:55
4

I found a solution, you just have to move the mouse to the coordinate (0,0) then you can move it to the place you want.

1

I wrote a class to do proper cursor positioning. This works under windows 10 scalings too.

Use the MoveMouseControlled(double, double) function to move the cursor to a specified position. It uses a [0,1] coordinate system. The (0,0) Point is the upper left corner of the screen.

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;

public class MouseCorrectRobot extends Robot
{
    final Dimension ScreenSize;// Primary Screen Size

    public MouseCorrectRobot() throws AWTException
    {
        super();
        ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
    }

    private static double getTav(Point a, Point b)
    {
        return Math.sqrt((double) ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
    }

    public void MoveMouseControlled(double xbe, double ybe)// Position of the cursor in [0,1] ranges. (0,0) is the upper left corner
    {

        int xbepix = (int) (ScreenSize.width * xbe);
        int ybepix = (int) (ScreenSize.height * ybe);

        int x = xbepix;
        int y = ybepix;

        Point mert = MouseInfo.getPointerInfo().getLocation();
        Point ElozoInitPont = new Point(0, 0);

        int UgyanAztMeri = 0;
        final int UgyanAZtMeriLimit = 30;

        int i = 0;
        final int LepesLimit = 20000;
        while ((mert.x != xbepix || mert.y != ybepix) && i < LepesLimit && UgyanAztMeri < UgyanAZtMeriLimit)
        {
            ++i;
            if (mert.x < xbepix)
                ++x;
            else
                --x;
            if (mert.y < ybepix)
                ++y;
            else
                --y;
            mouseMove(x, y);

            mert = MouseInfo.getPointerInfo().getLocation();

            if (getTav(ElozoInitPont, mert) < 5)
                ++UgyanAztMeri;
            else
            {
                UgyanAztMeri = 0;
                ElozoInitPont.x = mert.x;
                ElozoInitPont.y = mert.y;
            }

        }
    }

}
Gats János
  • 221
  • 1
  • 14
0

I just had a similar problem, to solve it I’ve just done a loop :

  • Test position
  • Move
  • Test position
  • if not OK move again

And it always works in less than 2 loops

Point pd = new Point(X,Y); // X,Y where mouse must go
int n = 0;
while ((!pd.equals(MouseInfo.getPointerInfo().getLocation())) && (++n <= 5))
{
    r.mouseMove(pd.x, pd.y);
}
Brydenr
  • 798
  • 1
  • 19
  • 30
0

It works well (correct location) in Full Screen mode with zoom=100%. press F-11 in chrome to full screen page.

MeirDayan
  • 620
  • 5
  • 20