Programming

(Java) Robot Class

steloflute 2012. 12. 8. 23:00

http://www.savthecoder.com/blog-java-robot-class#.UMNAU2f546w


About Robot



Robot is a class in Java, defined under the java.awt package. The Robot class helps you automate events like mouse moves and keyboard presses. It also has a handy feature which lets you take a picture of your screen.

Creating a Robot instance object



There are two constructors for the Robot class. One takes no parameters and call be called like this:


Robot robot = new Robot();


There's a also a constructor which takes in a GraphicsDevice object. This helps you define a specific device with graphics capabilities. For this article I'll be using the default constructor for code.

Mouse Events with Robot



You can do some nifty mouse events with Robot, like moving the mouse to a specific area on your screen, click a mouse button and use the mouse wheel.

#1. Move the mouse cursor



The mouseMove() method moves the mouse cursor to the screen coordinates you specify. The following code will move the cursor at the 400px by 400px position:


Robot robot = new Robot();
robot.mouseMove(400, 400);


Code practice: Make a simple project that moves your cursor slowly across the screen. See the code answer here.

#2. Click a mouse button



Use mousePress() and mouseRelease() methods to click and release the buttons on your mouse. Below is a code example:


Robot robot = new Robot();
robot.mousePress(MouseEvent.BUTTON1_MASK);
robot.mouseRelease(MouseEvent.BUTTON1_MASK);


The mouse button will remain clicked until until the mouseRelease() method is called.

Both methods take in an integer (int). A series of integers are defined by the MouseEvent class which you can use.

For mice with two buttons and a wheel on the center, these are the constant integers:

MouseEvent.BUTTON1_MASK: the left button
MouseEvent.BUTTON2_MASK: the scroll wheel
MouseEvent.BUTTON3_MASK: the right button

Code practice: Move the mouse to a button or icon and click it. See the code answer here.

#3: Move the mouse wheel



With the Robot class you can move the wheel on your mouse (if one exists) using the mouseWheel() method.


Robot robot = new Robot();
robot.mouseWheel(3);


mouseWheel() takes in a integer as a argument. This indicates the number of wheel notches to perform and the direction - a negative number indicates an up movement, a positive number indicates a down movement.

Keyboard Key Presses



Using Robot's keyPress() and keyRelease() methods, you can simulate a keyboard key pressing event.

The example below presses and releases the letter A on the keyboard:


Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);


Both methods take in an integer (int) to indicate which key is to be pressed. The KeyEvent class contains many static int variables to use on these methods. See the full list of integers in the KeyEvent documentation.

Take a photo of your screen.



You can use the Robot class to take a screenshot using the createScreenCapture() method. The method takes in a Rectangle object as its argument which should be the size of the screenshot you wish to take. Typically the rectangle would be the size of your screen.

See a code example of using createScreenCapture() to take screenshots