A Java Robot class mouse and keyboard/keystroke example

Java Robot class FAQ: Can you show me an example of how to use the Java Robot class?

Answer: Um, yeah, sure ... I say that a little jokingly. Okay, what really happened is that while developing this Java Robot example code on my Mac, I had to reboot it about 10 times. When you use the Java Robot class, you’re poking your head out into the native operating system, and if you mess up with your GUI events — at least on a Mac OS X system — a lot of bad things can happen.

Fortunately I just kept losing keyboard input, but I was still able to use the mouse, and was able to reboot my system easily using the mouse. So, as a word of warning, when you see some of these other Java Robot class examples out there on the internet — be careful — the correct solution is not as easy as any other Java Robot example I've seen out here.

That being said, the Java Robot example code here demonstrates how to use the mouse and generate keyboard keystrokes into other system windows with the Java Robot class, and I think that's pretty cool.

My Java Robot class

I won't describe this too much, because I think the Java code below speaks for itself. I do, however, want to point out several important "lessons learned":

  • You do want to use setAutoDelay
  • You do want to use setAutoWaitForIdle
  • You do want to use keyRelease after every keyPress

I simply could not get this Java Robot example code to work properly on my Mac OS X system without calling these Robot methods, as shown below, so I'm tempted to say you "must" use these settings, but I haven't tried this code on other operating systems yet. I do, however, know that I had problems running a similar version of this code on a Linux system without those three Robot code "rules".

My Java Robot class source code

Enough discussion for now, here's the source code for my Java Robot class, very fully tested on Mac OS X (macOS) 10.5 and 10.6 systems:

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

/**
 * A Java Robot example class.
 * 
 * Caution: Using the Java Robot class improperly can cause
 * a lot of system problems. I had to reboot my Mac ~10
 * times yesterday while trying to debug this code.
 * 
 * I created this class to demonstrate the Java Robot
 * class on a Mac OS X system, though it should run on Linux
 * or Windows as well.
 * 
 * On a Mac system, I place the TextEdit text editor in the 
 * upper-left corner of the screen, and put a bunch of blank lines 
 * in the editor. Then I run this Java Robot example from 
 * Eclipse or the Unix command line.
 * 
 * It types the three strings shown in the code below into
 * the text editor.
 *
 * Many thanks to the people on the Mac Java-dev mailing list
 * for your help. 
 * 
 * @author Alvin Alexander, https://alvinalexander.com
 *
 */
public class JavaRobotExample
{
  Robot robot = new Robot();

  public static void main(String[] args) throws AWTException
  {
    new JavaRobotExample();
  }
  
  public JavaRobotExample() throws AWTException
  {
    robot.setAutoDelay(40);
    robot.setAutoWaitForIdle(true);
    
    robot.delay(4000);
    robot.mouseMove(40, 130);
    robot.delay(500);

    leftClick();
    robot.delay(500);
    leftClick();

    robot.delay(500);
    type("Hello, world");

    robot.mouseMove(40, 160);
    robot.delay(500);

    leftClick();
    robot.delay(500);
    leftClick();
    
    robot.delay(500);
    type("This is a test of the Java Robot class");
    
    robot.delay(50);
    type(KeyEvent.VK_DOWN);
    
    robot.delay(250);
    type("Four score and seven years ago, our fathers ...");

    robot.delay(1000);
    System.exit(0);
  }
  
  private void leftClick()
  {
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(200);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(200);
  }
  
  private void type(int i)
  {
    robot.delay(40);
    robot.keyPress(i);
    robot.keyRelease(i);
  }

  private void type(String s)
  {
    byte[] bytes = s.getBytes();
    for (byte b : bytes)
    {
      int code = b;
      // keycode only handles [A-Z] (which is ASCII decimal [65-90])
      if (code > 96 && code < 123) code = code - 32;
      robot.delay(40);
      robot.keyPress(code);
      robot.keyRelease(code);
    }
  }
}

Java Robot class/example - Summary

I hope this Java Robot class example code is helpful. As mentioned, it demonstrates how to generate mouse and keyboard keystroke events into other system applications using the Java Robot class. For more information on the Robot class methods, see the Java Robot class Javadoc.

If you have any questions or suggestions just leave a note below. Again, please be very careful when using the Java Robot class on your computer system, as you can easily get your operating system in a bad state.