Example usage for java.awt Robot Robot

List of usage examples for java.awt Robot Robot

Introduction

In this page you can find the example usage for java.awt Robot Robot.

Prototype

public Robot() throws AWTException 

Source Link

Document

Constructs a Robot object in the coordinate system of the primary screen.

Usage

From source file:com.virtusa.isq.rft.runtime.RFTCommandBase.java

/**
 * Fires a set of java robot key events into the webpage.
 * //  w  ww .  j a  v  a  2 s .  com
 * @param commands
 *            the commands
 * @throws Exception
 *             the exception
 */

private void fireKeyEvent(final String commands) throws Exception {

    String[] commandSet = commands.split("\\|");
    Robot robot = new Robot();
    for (String fullCommand : commandSet) {
        Utils.pause(retryInterval / 2);
        int commandIndex = 0;
        int inputIndex = 1;
        String command = fullCommand.split("=")[commandIndex];
        String input = fullCommand.split("=")[inputIndex];
        if ("type".equalsIgnoreCase(command)) {
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            StringSelection stringSelection = new StringSelection(input);
            clipboard.setContents(stringSelection, null);

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);

        } else if ("Key".equalsIgnoreCase(command)) {

            type(input);
        } else if ("wait".equalsIgnoreCase(command)) {

            Utils.pause(Integer.parseInt(input));
        } else {
            throw new Exception("Command " + command);
        }
    }
}

From source file:faa.cucumber.pages.FaaHomePage.java

public void switchToNextTab() {
    waitABit(2000);//  ww w.  j av  a 2s.c  o m
    Robot robot;
    try {
        robot = new Robot();
        System.out.println("Switch to the Next Tab");
        robot.keyPress(KeyEvent.VK_CONTROL);
        System.out.println("Control Key Pressed");
        robot.keyPress(KeyEvent.VK_PAGE_DOWN);
        System.out.println("Page Down Key Pressed");
        robot.keyRelease(KeyEvent.VK_CONTROL);
        System.out.println("Control Key Released");
        robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
        System.out.println("Page Down Key Released");
        System.out.println("Next Browser Tab has been switched.");

    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.virtusa.isq.rft.runtime.RFTCommandBase.java

/**
 * Fires a set of java robot mouse events into the webpage.
 * //from   ww w .ja  va2  s . c  o m
 * @param commands
 *            the commands
 * @throws Exception
 *             the exception
 */

private void fireMouseEvent(final String commands) throws Exception {

    String[] commandSet = commands.split("\\|");
    Robot robot = new Robot();
    final int optimumPauseBetweenKeyCombs = 10;
    final int f11KeyCode = KeyEvent.VK_F11;
    for (String fullCommand : commandSet) {
        Utils.pause(retryInterval);
        int commandIndex = 0;
        int inputIndex = 1;
        String command = fullCommand.split("=")[commandIndex];
        String input = fullCommand.split("=")[inputIndex];

        if ("MOVE".equalsIgnoreCase(command)) {

            String[] coords = input.split(",");
            int resolutionWidth = Integer.parseInt(coords[0]);
            int resolutionHeight = Integer.parseInt(coords[inputIndex]);
            int x = Integer.parseInt(coords[inputIndex + 1]);
            int y = Integer.parseInt(coords[inputIndex + 2]);

            int xCordinateAutual = (int) calWidth(resolutionWidth, x);
            int yCordinateAutual = (int) calHight(resolutionHeight, y);

            robot.keyPress(f11KeyCode);
            robot.delay(optimumPauseBetweenKeyCombs);
            robot.keyRelease(f11KeyCode);
            Utils.pause(retryInterval);

            // Mouse Move
            robot.mouseMove(xCordinateAutual, yCordinateAutual);

            robot.keyPress(f11KeyCode);
            Utils.pause(optimumPauseBetweenKeyCombs);
            robot.keyRelease(f11KeyCode);

        } else if ("SCROLL".equalsIgnoreCase(command)) {

            robot.mouseWheel(Integer.parseInt(input));

        } else if ("wait".equalsIgnoreCase(command)) {

            Utils.pause(Integer.parseInt(input));
        } else {
            throw new Exception("Command " + command);
        }
    }
}

From source file:faa.cucumber.pages.FaaHomePage.java

public void closeFaaBrowserTabs() {
    waitABit(2000);//from   w ww . jav a 2s.  c o  m
    Robot robot;
    try {
        robot = new Robot();
        System.out.println("Close Tab");
        robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
        System.out.println("Control Key Pressed");
        robot.keyPress(java.awt.event.KeyEvent.VK_W);
        System.out.println("W Key Pressed");
        robot.keyRelease(java.awt.event.KeyEvent.VK_W);
        System.out.println("W Key Released");
        System.out.println("Control Key Released");
        robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:faa.cucumber.pages.FaaHomePage.java

public void switchFaaBrowserRefresh() {
    waitABit(2000);/*from   www.  ja  va  2  s.c om*/
    Robot robot;
    try {

        robot = new Robot();
        System.out.println("Refresh Browser Window");
        robot.keyPress(java.awt.event.KeyEvent.VK_F5);
        System.out.println("F5 Key Pressed");
        waitABit(1000);
        robot.keyRelease(java.awt.event.KeyEvent.VK_F5);
        System.out.println("F5 Key Released");
    } catch (AWTException e) {
        System.out.println("Error has occured when attempting to Refresh Browser Window!!");
        e.printStackTrace();
    }
}

From source file:faa.cucumber.pages.FaaHomePage.java

public void wKeyPress() {
    waitABit(2000);//w w  w .  j  a va2 s  .c om
    Robot robot;
    try {

        robot = new Robot();
        System.out.println("Press W Key");
        robot.keyPress(java.awt.event.KeyEvent.VK_W);
        System.out.println("W Key Pressed");
        waitABit(1000);
    } catch (AWTException e) {
        System.out.println("Error has occured when attempting to Refresh Browser Window!!");
        e.printStackTrace();
    }
}

From source file:faa.cucumber.pages.FaaHomePage.java

public void wKeyRelease() {
    waitABit(2000);/*  w  ww  .jav a 2s .  c o m*/
    Robot robot;
    try {

        robot = new Robot();
        System.out.println("Release W Key");
        robot.keyRelease(java.awt.event.KeyEvent.VK_W);
        System.out.println("W Key Release");
        waitABit(1000);
    } catch (AWTException e) {
        System.out.println("Error has occured when attempting to Refresh Browser Window!!");
        e.printStackTrace();
    }
}

From source file:com.virtusa.isq.rft.runtime.RFTCommandBase.java

/**
 * Do type./*from  w w w.  j av  a2  s . c o  m*/
 * 
 * @param keyCodes
 *            the key codes
 * @param offset
 *            the offset
 * @param length
 *            the length
 */
private void doTypeKeys(final int[] keyCodes, final int offset, final int length) {
    if (length == 0) {
        return;
    }
    try {
        Robot robot = new Robot();
        robot.keyPress(keyCodes[offset]);
        doTypeKeys(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.virtusa.isq.rft.runtime.RFTCommandBase.java

/**
 * Performs a Java robot click on the specific coordinates. <br>
 * //from   w w w  .  j  a  va  2s . c o  m
 * @param resolution
 *            the resolution
 * @param coordinates
 *            the coordinates
 * @param waitTime
 *            the wait time
 * @throws Exception
 *             the exception
 */
@Override
public final void mouseMoveAndClick(final String resolution, final String coordinates, final String waitTime) {

    String res = resolution;
    final int f11KeyCode = KeyEvent.VK_F11;
    final int optimumPauseBetweenkeyCombs = 10;
    String[] resArr = res.split(",");
    String[] coordinatesArr = coordinates.split(",");

    float screenWidht = Float.parseFloat(resArr[0]);
    float screeHigt = Float.parseFloat(resArr[1]);
    float xCordinate = Float.parseFloat(coordinatesArr[0]);
    float yCordinate = Float.parseFloat(coordinatesArr[1]);
    String command = "";

    if (coordinatesArr.length > 2) {

        command = coordinatesArr[2];
    }

    Robot robot = null;
    try {
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }

    Utils.pause(Integer.parseInt(waitTime));

    int xCordinateAutual = (int) calWidth(screenWidht, xCordinate);
    int yCordinateAutual = (int) calHight(screeHigt, yCordinate);

    robot.keyPress(f11KeyCode);
    robot.delay(optimumPauseBetweenkeyCombs);
    robot.keyRelease(f11KeyCode);
    Utils.pause(retryInterval);

    // Mouse Move
    robot.mouseMove(xCordinateAutual, yCordinateAutual);

    // Click
    if ("".equals(command)) {

        robot.mousePress(InputEvent.BUTTON1_MASK);
        Utils.pause(retryInterval);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);

        reportResults(ReportLogger.ReportLevel.SUCCESS, "Mouse Move And Click", "Success",
                "Resolution : " + res);

    } else if ("dclick".equals(command.toLowerCase(Locale.getDefault()))) {

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        final int optimumPauseBetweenDclick = 500;
        robot.delay(optimumPauseBetweenDclick);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);

        reportResults(true, ReportLogger.ReportLevel.SUCCESS, "Mouse Move And Click", "Success",
                "Resolution : " + res);

    }

    robot.keyPress(f11KeyCode);
    robot.delay(optimumPauseBetweenkeyCombs);
    robot.keyRelease(f11KeyCode);
}

From source file:gui.GW2EventerGui.java

private void preventSleepMode() {

    Thread t = new Thread() {

        @Override/*from www .  j  ava 2  s .co  m*/
        public void run() {
            try {
                Point mouseLoc;
                Robot rob = new Robot();

                while (true) {
                    try {
                        Thread.sleep(55000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(GW2EventerGui.class.getName()).log(Level.SEVERE, null, ex);
                        this.interrupt();
                    }

                    if (preventSystemSleep) {
                        mouseLoc = MouseInfo.getPointerInfo().getLocation();
                        rob.mouseMove(mouseLoc.x, mouseLoc.y);
                    }
                }
            } catch (AWTException ex) {
                Logger.getLogger(GW2EventerGui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

    t.start();
}