Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

DavidTheExplorer/Modern-Robot

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A modern replacement to the rusty java.awt.Robot

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.