LoginSignup
3
2

More than 5 years have passed since last update.

ロボットを作ろう!「Java AWT Robotの簡単なデモ」

Last updated at Posted at 2019-04-08

ロボット

辞書で、ロボットは「機械として組み立てられ、人間に似た種々の動作機能を発揮するもの」です。
それですが、ハードウェアだけではなく、ソフトウェアでも、人間っぽいな動きがあったら、それもロボットです。

それでは、今回の活動は、ソフトウェアロボットを作りましょう!

Java AWT Robot

色々なプラットフォームがありますが、今回はソフトウェアロボットにはJava AWTを使うことにします。
Java AWT (Abstract Window Toolkit)では、JavaでGUIまたはWindowベースのアプリケーションを開発するためのAPIです。AWTでRobot APIがあります。OSのNativeなか関数をこのRobotで自動できます。

ロボットの使い方

キーボード押す:

MyRobot.java
//Enterキーを押す
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

マウス動き:

MyRobot.java
//マウスは場面のX:350,Y:400へ動く
robot.mouseMove(350,400);

遅延:

MyRobot.java
//1秒遅延
robot.delay(1000);

NiseAPI

使いやすくために、私は自分のAPIを作りました。
このリンクでフォークできる「無料」: https://github.com/CoralBeef-zz/Nise

それともここでコードを共有する:

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サンプル

サンプルをするために、私は自動的なTwitterで投稿するできるプログラムです。自分でやってみてください。

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);

結論

これほど簡単な機能を使って, OSに基づいて自動的なタスクで、たくさんの可能性がありますね。

これを使ってもっとクリエイティブなことができるのを楽しみにします!

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2