private void updateRobotAutoDelay() { robot.setAutoDelay(delayBetweenEvents); }
public class TestRobotKeys { private Robot robot; public static void main(String[] args) { new TestRobotKeys(); } public TestRobotKeys() { try { robot = new Robot(); robot.setAutoDelay(250); robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_1); robot.keyRelease(KeyEvent.VK_1); robot.keyPress(KeyEvent.VK_2); robot.keyRelease(KeyEvent.VK_2); robot.keyPress(KeyEvent.VK_3); robot.keyRelease(KeyEvent.VK_4); robot.keyRelease(KeyEvent.VK_ALT); } catch (AWTException ex) { ex.printStackTrace(); } } }
Robot robot = new Robot(); robot.setAutoDelay(50); // ms ...
/** * Initiate. * * @param delay * Delay in between events for the player. The higher the value * the slower the play. * @param offset * Reference point for relative locations. * @throws AWTException * If fails to initialise for playing. */ public MacroPlayer(int delay, Point offset) throws AWTException { this.robot = new Robot(); this.robot.setAutoDelay(delay); this.offset = offset; // Determine if require press/release delay this.isRequirePressReleaseDelay = (delay < MIN_PRESS_RELEASE_DELAY); }
/** Allow this to be adjusted, mostly for testing. */ public static void setAutoDelay(int ms) { initializeRobot(); ms = Math.min(60000, Math.max(0, ms)); if (eventMode == EM_ROBOT) robot.setAutoDelay(ms); autoDelay = ms; Log.debug("Auto delay set to " + ms); }
Robot bot = new Robot(); bot.setAutoDelay(1); int duration = 3000; long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < duration) { bot.keyPress(KeyEvent.VK_R); bot.keyRelease(KeyEvent.VK_R); }
Robot bot = new Robot(); bot.setAutoDelay(50); int duration = 3000; long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < duration) { bot.keyPress(KeyEvent.VK_R); } bot.keyRelease(KeyEvent.VK_R);
import java.awt.AWTException; import java.awt.Robot; public class TestRobotScroll { public static void main(String[] args) { try { Robot bot = new Robot(); bot.setAutoDelay(100); Thread.sleep(2000); System.out.println("++"); bot.mouseWheel(25); Thread.sleep(2000); System.out.println("--"); bot.mouseWheel(-25); } catch (Exception ex) { ex.printStackTrace(); } } }
public class Keyboard { private static Robot robot; public void pressEscape() throws TestException { if (!getSession().CanRun()) throw new TestException(InvalidStateMessage); try { robot = new Robot(); robot.setAutoDelay(time); robot.keyPress(KeyEvent.VK_ESCAPE); robot.setAutoDelay(time); robot.keyRelease(KeyEvent.VK_ESCAPE); } catch (AWTException e) { e.printStackTrace(); } } }
public static void setAutoDelay(ActionContext actionContext){ Thing self = (Thing) actionContext.get("self"); java.awt.Robot robot = (java.awt.Robot) self.doAction("getRobot", actionContext); Integer ms = (Integer) self.doAction("getMs", actionContext); robot.setAutoDelay(ms); }
Robot bot = new Robot(); bot.setAutoDelay(100); // I find a small delay between events helps a lot... bot.keyPress(KeyEvent.VK_A); bot.keyRelease(KeyEvent.VK_A);
Robot robot = new Robot(); robot.setAutoDelay(3000); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER);
Runtime.getRuntime().exec("notepad"); Robot r = new Robot(); r.setAutoDelay(100); r.delay(500); r.keyPress(KeyEvent.VK_H); r.keyPress(KeyEvent.VK_E); r.keyPress(KeyEvent.VK_L); r.keyPress(KeyEvent.VK_L); r.keyPress(KeyEvent.VK_O);
Robot r = null; try { r = new Robot(); } catch (AWTException e) { e.printStackTrace(); } ... r.setAutoDelay(30); r.keyPress(KeyEvent.VK_CONTROL); //then click the button r.keyRelease(KeyEvent.VK_CONTROL);
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; public class RobotTest { public static void main(String[] args) { Robot r = null; try { r = new Robot(); } catch (AWTException e) { e.printStackTrace(); } r.setAutoDelay(250); r.keyPress(KeyEvent.VK_WINDOWS); r.keyPress(KeyEvent.VK_Z); r.keyRelease(KeyEvent.VK_Z); r.keyRelease(KeyEvent.VK_WINDOWS); } }
Robot bot = new Robot(); bot.setAutoDelay(250); bot.keyPress(KeyEvent.SHIFT_KEY); bot.keyPress(KeyEvent.EQUALS); // Shift + "=" = + bot.keyRelease(KeyEvent.EQUALS); bot.keyRelease(KeyEvent.SHIFT_KEY);
Robot r = new Robot(); r.setAutoDelay(250); r.keyPress(KeyEvent.VK_WINDOWS); r.keyPress(KeyEvent.VK_D); r.keyRelease(KeyEvent.VK_D); r.keyRelease(KeyEvent.VK_WINDOWS);
Actions builder = new Actions(driver); Robot robot = new Robot(); robot.setAutoDelay(500); Action drag = builder.clickAndHold(element).moveToElement(target) .build(); drag.perform(); robot.keyPress(KeyEvent.VK_CONTROL); Action drop = builder.release().build(); drop.perform(); robot.keyRelease(KeyEvent.VK_CONTROL);
/** * Instantiates a new mouse. * * @throws AWTException * In case the {@link Robot} class could not be initialized. */ protected Mouse() throws AWTException { try { this.robot = new Robot(); this.robot.setAutoDelay(0); } catch (final AWTException e) { log.log(Level.SEVERE, "The mouse input could not be initialized."); throw e; } this.location = new Point2D.Double(Game.world().camera().getViewport().getCenterX(), Game.world().camera().getViewport().getCenterY()); this.lastLocation = this.location; this.sensitivity = Game.config().input().getMouseSensitivity(); this.grabMouse = true; }
import java.awt.AWTException; import java.awt.Robot; import static java.awt.event.KeyEvent.VK_ALT; import static java.awt.event.KeyEvent.VK_TAB; import static java.lang.Integer.parseInt; public class WindowSwitcher { public static void main(final String... args) throws AWTException { final int repeat = args.length != 0 ? parseInt(args[0]) : 1; final Robot robot = createRobot(); robot.keyPress(VK_ALT); for (int i = 0; i < repeat; i++) { robot.keyPress(VK_TAB); robot.keyRelease(VK_TAB); robot.delay(500); } robot.keyRelease(VK_ALT); } public static Robot createRobot() throws AWTException { final Robot robot = new Robot(); robot.setAutoWaitForIdle(true); robot.setAutoDelay(10); return robot; } }