001package arcommon;
002
003import java.awt.AWTException;
004import java.awt.Dimension;
005import java.awt.MouseInfo;
006import java.awt.Point;
007import java.awt.Rectangle;
008import java.awt.Robot;
009import java.awt.event.InputEvent;
010import java.awt.event.KeyEvent;
011import java.awt.image.BufferedImage;
012import java.util.Arrays;
013
014import javax.swing.JFrame;
015
016public class RobotUtil {
017    private static Robot robot;
018    private static Point baseLocation;
019    
020    public static void init() {
021        if (robot != null) {
022            return ;
023        }
024        try {
025            robot = new Robot();
026        } catch (AWTException e) {
027            throw new RuntimeException(e);
028        }
029
030        Point startLocation = getMouseLocation();
031        robot.mouseMove(0, 0);
032        baseLocation = getMouseLocation();
033        moveMouseTo(startLocation);
034    }
035
036    public static void delay(int milliseconds) {
037        init();
038        robot.delay(milliseconds);
039    }
040
041    public static void centerMouseOnFrame(JFrame frame) {
042        init();
043        Dimension frameSize = frame.getSize();
044        Point location = frame.getLocation();
045        int centerX = location.x + frameSize.width / 2;
046        int centerY = location.y + frameSize.height / 2;
047        robot.mouseMove(centerX, centerY);
048    }
049    
050    public static Point getMouseLocation() {
051        return MouseInfo.getPointerInfo().getLocation();
052    }
053
054    public static void moveMouseTo(Point point) {
055        moveMouseTo(point.x, point.y);
056    }
057    
058    public static void moveMouseTo(int x, int y) {
059        init();
060        robot.mouseMove(x - baseLocation.x, y - baseLocation.y);
061    }
062
063    public static void dragMouseTo(int x, int y, int button) {
064        int buttonMask = getButtonMask(button);
065        robot.mousePress(buttonMask);
066        robot.delay(50);
067        robot.mouseMove(x - baseLocation.x, y - baseLocation.y);
068        robot.delay(50);
069        robot.mouseRelease(buttonMask);
070        robot.delay(50);
071    }
072
073    private static int getButtonMask(int button) {
074        int buttonMask = 0;
075        switch (button) {
076            case 1 :
077                buttonMask = InputEvent.BUTTON1_DOWN_MASK;
078            break;
079            case 2 :
080                buttonMask = InputEvent.BUTTON2_DOWN_MASK;
081            break;
082            case 3 :
083                buttonMask = InputEvent.BUTTON3_DOWN_MASK;
084            break;
085            default :
086                throw new RuntimeException("" + button);
087        }
088        return buttonMask;
089    }
090    
091    public static void mousePress(int button) {
092        init();
093        int buttonMask = getButtonMask(button);
094        robot.mousePress(buttonMask);
095        robot.delay(50);
096        robot.mouseRelease(buttonMask);
097        robot.delay(50);
098    }
099    
100    public static void keyPress(int keyCode) {
101        keyPressLocal(null, keyCode);
102    }
103
104    public static void keyPress(int keyModifier, int keyCode) {
105        keyPressLocal(new int[] {keyModifier}, keyCode);
106    }
107    
108    public static void keyPress(int[] keyModifiers, int keyCode) {
109        if (keyModifiers == null) {
110            throw new RuntimeException("keyModifiers == null");
111        }
112        if (keyModifiers.length == 0) {
113            throw new RuntimeException("keyModifiers.length == 0");
114        }
115        keyPressLocal(keyModifiers, keyCode);
116    }
117    
118    private static void keyPressLocal(int[] keyModifiers, int keyCode) {
119        init();
120        if (keyModifiers != null) {
121            for (int i = 0; i < keyModifiers.length; i++) {
122                int keyModifier = keyModifiers[i];
123                if (keyModifier != KeyEvent.VK_SHIFT && keyModifier != KeyEvent.VK_CONTROL && keyModifier != KeyEvent.VK_ALT && keyModifier != KeyEvent.VK_ALT_GRAPH) {
124                    throw new RuntimeException("keyModifier != KeyEvent.VK_SHIFT && keyModifier != KeyEvent.VK_CONTROL && keyModifier != KeyEvent.VK_ALT  && keyModifier != KeyEvent.VK_ALT_GRAPH " + keyModifier + " " + i + " " + Arrays.toString(keyModifiers));
125                }
126                int indexOf = ArrayUtil.indexOf(keyModifiers, keyModifier);
127                if (indexOf != -1 && indexOf < i) {
128                    throw new RuntimeException("keyModifier " + keyModifier + " set twice in array " + Arrays.toString(keyModifiers));
129                }
130                robot.keyPress(keyModifier);
131                robot.delay(50);
132            }
133        }
134        robot.keyPress(keyCode);
135        robot.delay(50);
136        robot.keyRelease(keyCode);
137        robot.delay(50);
138        if (keyModifiers != null) {
139            for (int i = 0; i < keyModifiers.length; i++) {
140                int keyModifier = keyModifiers[i];
141                robot.keyRelease(keyModifier);
142                robot.delay(50);
143            }
144        }
145    }
146    
147    public static void type(String keys) {
148        init();
149        for (int i = 0; i < keys.length(); i++) {
150            robot.keyPress(keys.charAt(i));
151            robot.delay(25);
152            robot.keyRelease(keys.charAt(i));
153            robot.delay(25);
154        }
155    }
156
157    public static BufferedImage doScreenCapture(int x, int y, int width, int height) {
158        init();
159        BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(x, y, width, height));
160        return screenCapture;
161    }
162}