Skip to content

Latest commit

 

History

History
72 lines (56 loc) · 2.13 KB

File metadata and controls

72 lines (56 loc) · 2.13 KB

Modern Robot

The Java SDK has an ancient class called Robot that suffers from:

  • Boilerplate - You have to manage delays, remember to release keys in reverse order, handle CAPS LOCK...
  • Horrible Readability - Tapping even 1 key requires multiple lines full of bad constant names.

This library solves those problems behind a Mouse and Keyboard abstractions that internally utilize the robot.

Comparison to Java's Robot

Example scenario: We want to type "Hey", press enter, and click the mouse at (100, 100).

Modern Robot

//should be reused
Keyboard keyboard = HardwareFactory.createKeyboard();
Mouse mouse = HardwareFactory.createMouse();

keyboard.type("Hey");
keyboard.press(ENTER); //static import of KeyboardFunction

mouse.performAt(LEFT_CLICK, new Point2D(100, 100)); //static import of MouseAction

Java's Robot

I'm warning you...
Robot robot = new Robot(); //handle AWTException

//caps lock
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
smallDelay();

//type 'H'
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
smallDelay();

//caps lock
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
smallDelay();

//type 'e'
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
smallDelay();

//type 'y'
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
smallDelay();

//press enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
smallDelay();

//move and click the mouse
robot.mouseMove(100, 100);
robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);

Known Limitations

  1. The library cannot yet hold keyboard keys - it only types. This also means that special characters aren't supported because they require holding shift.
    Solving this issue is possible, but requires time that I simply don't have. Feel free to make a PR!
  2. Since this library is a fancy wrapper of the Java's Robot, it cannot differentiate between left and right keys(e.g. shift).