Let's make a robot! "A simple demo of Java AWT Robot"

robot

In the dictionary, a robot is "assembled as a machine and exhibits various human-like motion functions." However, if there is human-like movement not only in hardware but also in software, it is also a robot.

Let's make a software robot for this activity!

Java AWT Robot

There are various platforms, but this time I will use Java AWT for the software robot. Java AWT (Abstract Window Toolkit) is an API for developing GUI or Window-based applications in Java. There is a Robot API in AWT. Functions in Native of the OS can be automated with this Robot.

How to use the robot

Keyboard press:

MyRobot.java


//Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Mouse movement:

MyRobot.java


//Mouse is the scene X:350,Y:Move to 400
robot.mouseMove(350,400);

delay:

MyRobot.java


//1 second delay
robot.delay(1000);

NiseAPI

For ease of use, I created my own API. "Free" that can be forked with this link: https://github.com/CoralBeef-zz/Nise

Or share the code here:

NiseRobot.java


import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

public class NiseRobot extends Robot {


    public NiseRobot() throws AWTException {}

    public void delayedKeyPress(int keyToPress) {
        delayedKeyPress(keyToPress,500);
    }

    public void delayedKeyPress(int keyToPress, int delay_time) {
        delay(delay_time);
        keyPress(keyToPress);
        keyRelease(keyToPress);
    }

    public void instantType(String text) {
        StringSelection stringSelection = new StringSelection(text);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, stringSelection);

        keyPress(KeyEvent.VK_CONTROL);
        keyPress(KeyEvent.VK_V);
        keyRelease(KeyEvent.VK_V);
        keyRelease(KeyEvent.VK_CONTROL);
    }

    public void waitFor(int delay_in_milliseconds) {
        try {
            Thread.sleep(delay_in_milliseconds);
        } catch(InterruptedException ioe) {}
    }

    public void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) {
        try {
            double dx = (x2 - x1) / ((double) n);
            double dy = (y2 - y1) / ((double) n);
            double dt = t / ((double) n);
            for (int step = 1; step <= n; step++) {
                Thread.sleep((int) dt);
                mouseMove((int) (x1 + dx * step), (int) (y1 + dy * step));
            }
        } catch (InterruptedException e) {}
    }
}

TwitterBot sample

To sample, I'm a program that can automatically post on Twitter. Try it yourself.

Nise.start()


String tweet = JOptionPane.showInputDialog(new JFrame("NameFrame"), "What do you want to tweet?");

        robot.delayedKeyPress(KeyEvent.VK_WINDOWS);

        robot.delayedKeyPress(KeyEvent.VK_C);
        robot.delayedKeyPress(KeyEvent.VK_H);
        robot.delayedKeyPress(KeyEvent.VK_R);
        robot.delayedKeyPress(KeyEvent.VK_O);
        robot.delayedKeyPress(KeyEvent.VK_M);
        robot.delayedKeyPress(KeyEvent.VK_E);

        robot.delayedKeyPress(KeyEvent.VK_ENTER);

        robot.mouseGlide(500,500, 600, 60, 8, 1000);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);


        robot.delayedKeyPress(KeyEvent.VK_H);
        robot.delayedKeyPress(KeyEvent.VK_T);
        robot.delayedKeyPress(KeyEvent.VK_T);
        robot.delayedKeyPress(KeyEvent.VK_P);
        robot.delayedKeyPress(KeyEvent.VK_S);

        //Colon
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_SEMICOLON);
        robot.keyRelease(KeyEvent.VK_SEMICOLON);
        robot.keyRelease(KeyEvent.VK_SHIFT);

        robot.delayedKeyPress(KeyEvent.VK_SLASH);
        robot.delayedKeyPress(KeyEvent.VK_SLASH);
        robot.delayedKeyPress(KeyEvent.VK_T);
        robot.delayedKeyPress(KeyEvent.VK_W);
        robot.delayedKeyPress(KeyEvent.VK_I);
        robot.delayedKeyPress(KeyEvent.VK_T);
        robot.delayedKeyPress(KeyEvent.VK_T);
        robot.delayedKeyPress(KeyEvent.VK_E);
        robot.delayedKeyPress(KeyEvent.VK_R);
        robot.delayedKeyPress(KeyEvent.VK_PERIOD);
        robot.delayedKeyPress(KeyEvent.VK_C);
        robot.delayedKeyPress(KeyEvent.VK_O);
        robot.delayedKeyPress(KeyEvent.VK_M);
        robot.delayedKeyPress(KeyEvent.VK_SLASH);
        robot.delayedKeyPress(KeyEvent.VK_ENTER);

        robot.delayedKeyPress(KeyEvent.VK_ENTER);

        robot.delay(3000);
        robot.mouseGlide(600,60, 750, 230, 8, 1000);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        robot.instantType(tweet);

        robot.mouseGlide(750,230, 1250, 360, 8, 1000);

        robot.delay(3000);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

Conclusion

With such a simple function, it is an automatic task based on the OS, and there are many possibilities.

I look forward to doing more creative things with this!

Recommended Posts

Let's make a robot! "A simple demo of Java AWT Robot"
[Java] Let's make a DB access library!
A collection of simple questions for Java beginners
Make "I'm not a robot" in Java EE (Jakarta EE)
[Java basics] Let's make a triangle with a for statement
[Personal memo] Make a simple deep copy in Java
[Java] Make it a constant
[Java] Draw a simple pattern
Make a rhombus using Java
Let's make a calculator application in Java ~ Display the application window
[Beginner] Try to make a simple RPG game with Java ①
Let's make a simple API with EC2 + RDS + Spring boot ①
I tried to make a client of RESAS-API in Java
Think of a Java update strategy
Let's create a TODO application in Java 4 Implementation of posting function
Make a language! (Making a simple calculator ②)
Try to make a simple callback
3 Implement a simple interpreter in Java
How to make a Java container
Let's create a TODO application in Java 6 Implementation of search function
[Java] Let's take a look at Switch Expressions (Preview) of JDK 13.
Let's create a TODO application in Java 8 Implementation of editing function
Sort a List of Java objects
A simple sample callback in Java
Make a language! (Making a simple calculator ①)
A brief description of JAVA dependencies
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
Let's make a custom_cop that points out the shaking of the name
How to make a Java array
Let's express the result of analyzing Java bytecode with a class diagram
I was addicted to a simple test of Jedis (Java-> Redis library)
Java beginner tried to make a simple web application using Spring Boot
How to make a Java calendar Summary
Let's make a Christmas card with Processing!
Name a group of regular expressions (Java)
A simple sample of ArBiMap (two-way map)
Let's create a Java development environment (updating)
How to make a Discord bot (Java)
Let's make a smart home with Ruby!
[docker] [nginx] Make a simple ALB with nginx
[Rails] Let's create a super simple Rails API
Let's migrate to make Java more comfortable
A simple example of an MVC model
Let's make a Force-Graph of railway network from Tokyo public transportation open data
Let's make a calculator application with Java ~ Create a display area in the window
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
A simple example of a Servlet that displays Japanese
Create a simple bulletin board with Java + MySQL
Do you need a memory-aware implementation of Java?
Let's create a timed process with Java Timer! !!
Make a margin to the left of the TextField
java I tried to break a simple block
Measure the size of a folder in Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
Let's make a shopping site using stripe! (Purchase)
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
Create Scala Seq from Java, make Scala Seq a Java List
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
Let's make a search function with Rails (ransack)