6 Replies Latest reply on Mar 11, 2010 4:07 PM by 843807

    Robot not working after another app gains focus

      I'm trying to automate some clicking for a program using Robot. The problem is, once I give focus to the target program, Robot stops working (so basically, the initial click into the program works, but it's useless if I can't release the mouse). Intuitively, it seems like the target program has somehow gained a higher priviledge to control the mouse or something if it has focus. Programmatically giving focus back to the java program after every event won't work. Is there a way to fix this, or should I try something more native to Windows like C#?
      This target program has a 3D interface and it uses DirectX and stuff, which might be why it (can) eats up control of the mouse. However I just think it's badly programmed, I tried doing some Robot stuff on a game and it seems to work.
        • 1. Re: Robot not working after another app gains focus
          To get better help sooner, post a [_SSCCE_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.

          Use code tags to post codes -- [code]CODE[/code] will display as
          Or click the CODE button and paste your code between the {code} tags that appear.

          I'm moving this thread to a more appropriate forum.

          • 2. This Thread is now moved
            Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
            • 3. Re: Robot not working after another app gains focus
              Well, any sort of Robot code that does mouse stuff would work but here's a part of my code:
                        Robot rr;
                             rr = new Robot();
                             rr.mouseMove(620, 700);
                        catch (AWTException e)
                        catch (InterruptedException e)
              In this case, the cursor will move to 620,700 and the initial mousePress will work, but the release (and anything after that) won't.
              • 4. Re: Robot not working after another app gains focus

                How are you checking to see if it works? If you have an application that is processing the click events, and another application covers it, then it will no longer see the click events.

                This code:
                import java.awt.AWTException;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.awt.event.InputEvent;
                import java.awt.Point;
                import java.awt.Robot;
                import javax.swing.Timer;
                public class JRobot implements ActionListener{
                  Robot r;
                  Timer t = new Timer(200, this);
                  Point p = new Point(512, 512);
                  boolean bTime = false;
                  int iCount = 0;
                  public JRobot(){
                      r = new Robot();
                    }catch(AWTException e){
                  public static void main(String[] args) {
                    new JRobot();
                  public void actionPerformed(ActionEvent e){
                      p.x = 256;
                      p.y = 256;
                      p.x = 512;
                      p.y = 512;
                    int iState = iCount%6;
                      case 1: r.mouseMove(p.x, p.y);
                      case 2: r.mousePress(InputEvent.BUTTON1_MASK);
                      case 4: r.mouseRelease(InputEvent.BUTTON1_MASK);
                      case 5: bTime = !bTime;
                    if(iCount>=100) System.exit(0);
                    System.out.println(iState+" x " + p.x + " y "+ p.y);
                Does keep running and does click and release. I suspect your's is also. If you put one object at (256, 256) and another at (512, 512) you will soon see that they are alternatly chosen and gain and release focus appropriatly, but if one object is on top of the other, then the top object will recieve the click events.
                • 5. Re: Robot not working after another app gains focus
                  Well, I can tell that Robot is no longer 'working' becuase nothing happens after the target app gains focus. Perhaps I should clarify the situation a little more.
                  There is the target application, which needs to have certain things clicked and whatnot. It's running and takes up most of the screen.
                  I start up the java app (say the one you just posted), and it creates a new Robot. OK.
                  It moves the mouse to 512,512. OK. At this point, the focus is still on the java app I just started up (eclipse or a commandline window).
                  It does a mousePress. At this point, the target application gains focus and recieves the mousedown event (if there is a button in the target app, it gets depressed).
                  It does a mouseRelease. Nothing happens (the button is still depressed).
                  it does a mouseMove to 256, 256. The cursor does not move. The button is still depressed. After this, Robot doesn't 'work'.
                  If I minimize the target app or make it lose focus and try to give focus back to the java app, sometimes Robot will begin working again; I haven't quite figured out what will make Robot regain control.
                  • 6. Re: Robot not working after another app gains focus
                    On that I've not a clue, I run in Solaris 10, Windows XP and 2003 server, and Ubuntu (what ever the flavor of the day is) and I cannot reproduce the problem you describe. Post a working example of your code that displays the problem you describe. Also try it on another machine and see if it's your local box only.