简体   繁体   中英

Java: mousePress() does not always work

I have made a simple utility program in Java that makes the mouse click wherever it is, once every X milliseconds:

import java.awt.Robot;
import java.awt.event.InputEvent;

public class Main 
{
    public static final int SLEEP_TIME = 60000;

    public static void main(String[] args)
    {
        try
        {
            Robot r = new Robot();
            int i = 0;
            while(true)
            {
                doNothing();
                System.out.println(++i + ") Pressed");
                r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void doNothing() 
    {
        long endTime = System.currentTimeMillis() + SLEEP_TIME;
        while(System.currentTimeMillis() < endTime)
        {
            //Do nothing
        }
    }
}

I have made it because for external reasons there is a button in an installer that I have to press every X minutes because there is a failure in it.

So this little program works fine everywhere I am (desktop, Chrome, etc.) EXCEPT FOR THE INSTALLER.

The button I have to press is like this, "Reanudar": See here

So in Eclipse it appears that the action is done (it prints a message everytime) but there it just does not work.

Some help please? Thanks in advance.

The two major issues I have seen when using those methods are as follows:

  1. Delay between press and release. Add a slight delay and I imagine it is probably going to work a lot better.
  2. The application itself has something that blocks this type of input. This is especially common on larger applications (especially things that outright reject the use of bots/macros). I doubt this is the issue but might be worth looking into depending on the application.

I'd start here, and see what you can find. At a minimum add some delays and post the results for more discussion.

FOUND THE SOLUTION! A friend of mine succesfully pointed out that, as this installer is running as Administrator, it might not be clickable by a non-Administrator Eclipse program. So I ran Eclipse and my program as Administrator and IT WORKED!

Thanks for those couple of answers I got, hope mine solves someone else's related problems!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM