Java robot class mouseMove not going to specified location
Asked Answered
N

1

4

To be honest I am not entirely sure what is wrong. This is the short version of a ton of other basic robot command movements under the if and if else.

Whenever I run the program the mouse should move to the designated position and click. However when I run the program it does not move to the position I indicated, instead it moves to a different position each time I run it(I do not have any listeners designated to change the position so the position shouldn't be changing). I do not know if it is something with the code I have written itself or possibly my imports? The program was running correctly until recently in which I added the else at the end to end the program, I have run it without the else and still come up with the same issue. Any help would be much appreciated.

package creator;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

public class RobotDemo extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;


public static void main(String[] args) throws AWTException, IOException
{

             double value = (-0.66721);
        {
             if (value < -0.3)
             {                                          
                    Robot robotdelta = new Robot();
                    //Enters Chrome from java
                    robotdelta.delay(5000);                     
                    robotdelta.mouseMove(587, 1045);                       
                    robotdelta.delay(1000);
                    robotdelta.mousePress(InputEvent.BUTTON1_MASK);     
                    robotdelta.delay(1000);     
                    robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);       
                    robotdelta.delay(1000);
                    //Enters online platfrom
                    robotdelta.mouseMove(770, 21);
                    robotdelta.delay(1000);
                    robotdelta.mousePress(InputEvent.BUTTON1_MASK);     
                    robotdelta.delay(1000);     
                    robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);       
                    robotdelta.delay(1000);                                                                                                                 
                 }


             //secondary situation 
             else if (value > 0.3)
             {                                  
                    Robot robotdelta = new Robot();
                    //Enters Chrome from java
                    robotdelta.delay(1000);
                    robotdelta.mouseMove(587, 1045);
                    robotdelta.delay(100);
                    robotdelta.mousePress(InputEvent.BUTTON1_MASK);     
                    robotdelta.delay(100);      
                    robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);       
                    robotdelta.delay(100);
                    //Enters online platfrom
                    robotdelta.mouseMove(770, 21);
                    robotdelta.delay(100);
                    robotdelta.mousePress(InputEvent.BUTTON1_MASK);     
                    robotdelta.delay(100);      
                    robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);       
                    robotdelta.delay(1000);

             }
             else
             {
                system.exit(0);
             }

             }


 }    





public void actionPerformed(ActionEvent e) {


}       



}
Nit answered 28/11, 2017 at 18:30 Comment(0)
P
1

As a disclaimer, I was playing around with this class for a while, and the most important thing I learned was this was a tool meant for very rudimentary testing, and really no large scale crucial operation should ever hinge on this class working exactly as expected.

To answer your question, there is really no way to get exactly where you tell mouseMove() to go (at least not when I was working with it). However, what seemed to get it pretty close was to call mouseMove() multiple times to the same place (Yes, this is very hacky and not desirable). For example, is I wanted to move the mouse to (300,600) on screen, I found that if you do:

mouseMove(300,600);
mouseMove(300,600);
mouseMove(300,600);
// ... can put more if you want

for some strange reason it gets much closer to where you want to go than if you just call mouseMove() one time. I have no idea why this might be the case, but hopefully this helps. Not to mention, it is also a good idea to put ample delays in between calling the robot to do different actions, and to ensure that waitForIdle() is invoked.

Perfunctory answered 13/8, 2018 at 18:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.