How do I create mouse event using Robot class?

In this example we are automating the process of creating mouse event using the java.awt.Robot class.

package org.kodejava.awt;

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

public class MovingMouseDemo {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // Move mouse cursor to 200, 500
            robot.mouseMove(200, 500);

            // Press the mouse button #1.
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

            // Scroll the screen down for a mouse with a wheel support.
            robot.mouseWheel(150);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}

How do I get the colour of a screen pixel?

The example here show us how to get the color of a pixel in the screen. We use the Robot.getPixelColor(int x, int y) method to obtain the Color of the pixel.

package org.kodejava.awt;

import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;

public class ColorPickerDemo {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // The pixel color information at 20, 20
            Color color = robot.getPixelColor(200, 200);

            // Print the RGB information of the pixel color
            System.out.println("Red   = " + color.getRed());
            System.out.println("Green = " + color.getGreen());
            System.out.println("Blue  = " + color.getBlue());

        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}

Example output of code snippet above:

Red   = 35
Green = 38
Blue  = 53

How do I create key press event using Robot class?

In this example we use the java.awt.Robot class to generate a key press event. We can call the keyPress(int keyCode) method to produce this event.

package org.kodejava.awt;

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

public class CreatingKeyboardEvent {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // Create a three seconds delay.
            robot.delay(3000);

            // Generating key press event for writing the QWERTY letters
            robot.keyPress(KeyEvent.VK_Q);
            robot.keyPress(KeyEvent.VK_W);
            robot.keyPress(KeyEvent.VK_E);
            robot.keyPress(KeyEvent.VK_R);
            robot.keyPress(KeyEvent.VK_T);
            robot.keyPress(KeyEvent.VK_Y);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}

How do I create a screen capture using Robot class?

In this example you’ll see how to create a screen capture / screenshot and save it as an image file such a PNG image. Some classes are use in this program including the java.awt.Robot, java.awt.image.BufferedImage and javax.imageio.ImageIO.

package org.kodejava.awt;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;

public class ScreenCapture {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // Capture screen from the top left in 200 by 200 pixel size.
            BufferedImage bufferedImage = robot.createScreenCapture(
                    new Rectangle(new Dimension(200, 200)));

            // The captured image will the written into a file called
            // screenshot.png
            File imageFile = new File("screenshot.png");
            ImageIO.write(bufferedImage, "png", imageFile);
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }
}