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.
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) {}
}
}
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);
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