Search code examples
javaui-automationawtrobot

Use robot to click and type in Microsoft Word not working


I created the following code to do the following:

  1. Create a new Word file and open it
  2. Check the task manager to see if it did open and then continue
  3. Use a Robot class to move to the centre of the screen, then click on the Word application and type: Y.

It is doing everything except typing Y.

Code:

// Open Word Document
public boolean openWordDoc(String tempProName, String URL, String j) throws IOException, AWTException {

    boolean result = false;

    String userHomeFolder = System.getProperty("user.home");
    File textFile = new File((userHomeFolder + "/Desktop"), j + " " + URL);
    BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
    out.close();
    if (Desktop.isDesktopSupported()) {
        Desktop fD = Desktop.getDesktop();
        fD.open(textFile);
    }

    // See if it really did open and wait until it opened to continue
    String line = "";
    Process p = Runtime.getRuntime().exec("tasklist.exe");
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((!line.equals("WINWORD.EXE"))) {
        while ((line = input.readLine()) != null) {
            String[] templine = line.split(" ");
            if (templine[0].equals("WINWORD.EXE")) {
                line = "WINWORD.EXE"; 
                result = true;
                break;
            }
        }
    }

    // Robot
    Robot robot = new Robot();

    double x = 0;
    double y = 0;

    x = Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
    y = Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;

    int xx = (int) Math.round(x);
    int yy = (int) Math.round(y);

    robot.mouseMove(xx, yy);

    robot.mousePress(InputEvent.BUTTON1_MASK);

    robot.keyPress(KeyEvent.VK_Y);

    return result;
}

Solution

  • As a popup window always opens in the middle of the screen it's safe to say that:

    Dimension middle = new Dimension(Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
    

    Returns the excact middle of the screen. Now you'd have to move the cursor relative to the middle to where the button is. To do this you could just try over and over again, but you could also set up a while loop that tells you where the button is. Like this:

    while(true){
        System.out.println(MouseInfo.getPointerInfo().getLocation());
    }
    

    Now you know where the button is on the screen but now you should make it relative to the middle so that this will work on any screen any size. Therefore you'd have to take the difference in x and y. Now that you know where the button is relative to the middle you can say:

    Robot robot = new Robot();
    robot.mouseMove(middle.getWidth() + differenceX, middle.getHeight() + differenceY);
    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    

    Note that this way is not entirely safe. If the user manages to move the popup before it's clicked the code will click somewhere else.

    But for the rest I belive that this code will work and on all monitors. But I haven't tested it. And you may need to let the robot click twice once to gain focus and an other time to actually click. If you have any more questions don't hesitate to ask them.

    I hope this works.

    Key press code:

    // Robot
        Robot robot = new Robot();
    
        double x = 0;
        double y = 0;
    
        x = Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
        y = Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;
    
        int xx = (int) Math.round(x);
        int yy = (int) Math.round(y);
    
        robot.mouseMove(xx, yy);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        robot.keyPress(KeyEvent.VK_Y);
    

    You should be able to see where it goes.
    I hope this works :).