|
| 1 | +package com.annimon.ownlang.lib.modules; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.ArgumentsMismatchException; |
| 4 | +import com.annimon.ownlang.lib.*; |
| 5 | +import java.awt.AWTException; |
| 6 | +import java.awt.Robot; |
| 7 | +import java.awt.event.InputEvent; |
| 8 | +import java.awt.event.KeyEvent; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @author aNNiMON |
| 15 | + */ |
| 16 | +public final class robot implements Module { |
| 17 | + |
| 18 | + private static final int CLICK_DELAY = 200; |
| 19 | + private static final int TYPING_DELAY = 50; |
| 20 | + |
| 21 | + private static final Map<Character, Integer> SYMBOL_CODES; |
| 22 | + static { |
| 23 | + SYMBOL_CODES = new HashMap<>(10); |
| 24 | + SYMBOL_CODES.put('_', KeyEvent.VK_MINUS); |
| 25 | + SYMBOL_CODES.put(':', KeyEvent.VK_SEMICOLON); |
| 26 | + } |
| 27 | + |
| 28 | + private static Robot awtRobot; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void init() { |
| 32 | + initialize(); |
| 33 | + |
| 34 | + Functions.set("click", convertFunction(robot::click)); |
| 35 | + Functions.set("delay", convertFunction(awtRobot::delay)); |
| 36 | + Functions.set("setAutoDelay", convertFunction(awtRobot::setAutoDelay)); |
| 37 | + Functions.set("keyPress", convertFunction(awtRobot::keyPress)); |
| 38 | + Functions.set("keyRelease", convertFunction(awtRobot::keyRelease)); |
| 39 | + Functions.set("mousePress", convertFunction(awtRobot::mousePress)); |
| 40 | + Functions.set("mouseRelease", convertFunction(awtRobot::mouseRelease)); |
| 41 | + Functions.set("mouseWheel", convertFunction(awtRobot::mouseWheel)); |
| 42 | + Functions.set("mouseMove", (args) -> { |
| 43 | + if (args.length != 2) throw new ArgumentsMismatchException("Two arguments expected"); |
| 44 | + try { |
| 45 | + awtRobot.mouseMove((int) args[0].asNumber(), (int) args[1].asNumber()); |
| 46 | + } catch (IllegalArgumentException iae) { } |
| 47 | + return NumberValue.ZERO; |
| 48 | + }); |
| 49 | + Functions.set("typeText", (args) -> { |
| 50 | + if (args.length != 1) throw new ArgumentsMismatchException("One argument expected"); |
| 51 | + try { |
| 52 | + typeText(args[0].asString()); |
| 53 | + } catch (IllegalArgumentException iae) { } |
| 54 | + return NumberValue.ZERO; |
| 55 | + }); |
| 56 | + |
| 57 | + Variables.set("VK_DOWN", new NumberValue(KeyEvent.VK_DOWN)); |
| 58 | + Variables.set("VK_LEFT", new NumberValue(KeyEvent.VK_LEFT)); |
| 59 | + Variables.set("VK_RIGHT", new NumberValue(KeyEvent.VK_RIGHT)); |
| 60 | + Variables.set("VK_FIRE", new NumberValue(KeyEvent.VK_ENTER)); |
| 61 | + Variables.set("VK_ESCAPE", new NumberValue(KeyEvent.VK_ESCAPE)); |
| 62 | + |
| 63 | + Variables.set("BUTTON1", new NumberValue(InputEvent.BUTTON1_MASK)); |
| 64 | + Variables.set("BUTTON2", new NumberValue(InputEvent.BUTTON2_MASK)); |
| 65 | + Variables.set("BUTTON3", new NumberValue(InputEvent.BUTTON3_MASK)); |
| 66 | + } |
| 67 | + |
| 68 | + private static void initialize() { |
| 69 | + try { |
| 70 | + awtRobot = new Robot(); |
| 71 | + } catch (AWTException awte) { |
| 72 | + throw new RuntimeException("Unable to create robot instance", awte); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @FunctionalInterface |
| 77 | + private interface RobotIntConsumer { |
| 78 | + void accept(int value) throws IllegalArgumentException; |
| 79 | + } |
| 80 | + |
| 81 | + private static Function convertFunction(RobotIntConsumer consumer) { |
| 82 | + return args -> { |
| 83 | + if (args.length != 1) throw new ArgumentsMismatchException("One argument expected"); |
| 84 | + try { |
| 85 | + consumer.accept((int) args[0].asNumber()); |
| 86 | + } catch (IllegalArgumentException iae) { } |
| 87 | + return NumberValue.ZERO; |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + private static synchronized void click(int buttons) { |
| 92 | + awtRobot.mousePress(buttons); |
| 93 | + awtRobot.delay(CLICK_DELAY); |
| 94 | + awtRobot.mouseRelease(buttons); |
| 95 | + } |
| 96 | + |
| 97 | + private static synchronized void typeText(String text) { |
| 98 | + for (char ch : text.toCharArray()) { |
| 99 | + typeSymbol(ch); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static void typeSymbol(char ch) { |
| 104 | + int code = KeyEvent.getExtendedKeyCodeForChar(ch); |
| 105 | + |
| 106 | + boolean isUpperCase = Character.isLetter(ch) && Character.isUpperCase(ch); |
| 107 | + boolean needPressShift = isUpperCase; |
| 108 | + if (!isUpperCase) { |
| 109 | + final int symbolIndex = "!@#$%^&*()".indexOf(ch); |
| 110 | + if (symbolIndex != -1) { |
| 111 | + needPressShift = true; |
| 112 | + code = '1' + symbolIndex; |
| 113 | + } else if (SYMBOL_CODES.containsKey(ch)) { |
| 114 | + needPressShift = true; |
| 115 | + code = SYMBOL_CODES.get(ch); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (code == KeyEvent.VK_UNDEFINED) return; |
| 120 | + |
| 121 | + if (needPressShift) { |
| 122 | + // press shift |
| 123 | + awtRobot.keyPress(KeyEvent.VK_SHIFT); |
| 124 | + awtRobot.delay(TYPING_DELAY); |
| 125 | + } |
| 126 | + |
| 127 | + awtRobot.keyPress(code); |
| 128 | + awtRobot.delay(TYPING_DELAY); |
| 129 | + awtRobot.keyRelease(code); |
| 130 | + |
| 131 | + if (needPressShift) { |
| 132 | + // release shift |
| 133 | + awtRobot.delay(TYPING_DELAY); |
| 134 | + awtRobot.keyRelease(KeyEvent.VK_SHIFT); |
| 135 | + awtRobot.delay(TYPING_DELAY); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments