Example usage for java.awt Robot keyPress

List of usage examples for java.awt Robot keyPress

Introduction

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

Prototype

public synchronized void keyPress(int keycode) 

Source Link

Document

Presses a given key.

Usage

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

public void pressShiftKey() {
    waitABit(2000);/*from  w  w  w.  j a v a  2 s. c  om*/
    Robot robot;
    try {
        robot = new Robot();
        System.out.println("Keypress Shift Key to Open a New Browser");
        robot.keyPress(KeyEvent.VK_SHIFT);
        System.out.println("Shift Key Pressed");
        //         robot.keyPress(KeyEvent.VK_TAB);   
        //         System.out.println("Shift Key Pressed" );
        //         robot.keyRelease(KeyEvent.VK_CONTROL);
        //         System.out.println("Control Key Released" );
        //         robot.keyRelease(KeyEvent.VK_TAB);   
        //         System.out.println("TAB Key Released" );
        //         System.out.println("Browser Tabs have been switched.");

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

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

public void switchFaaBrowserTabs() {
    waitABit(2000);//from   ww w.ja v a 2s  .c  om
    Robot robot;
    try {

        //         //Navigate from Left to Right
        //         Actions action= new Actions(driver);
        //         action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

        robot = new Robot();
        System.out.println("Switching Tabs");
        robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
        System.out.println("Control Key Pressed");
        waitABit(1000);
        robot.keyPress(java.awt.event.KeyEvent.VK_TAB);
        System.out.println("TAB Key Pressed");
        robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);
        System.out.println("TAB Key Released");
        robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
        System.out.println("Control Key Released");

        //         robot.keyPress(java.awt.event.KeyEvent.VK_TAB);   
        //         waitABit(1000);
        //         System.out.println("TAB Key Pressed..Second Time" );
        //         robot.keyPress(java.awt.event.KeyEvent.VK_TAB);   
        //         System.out.println("TAB Key Pressed..third Time" );
        //         waitABit(1000);

        //         System.out.println("Switching Tabs Again" );
        //         robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
        //         System.out.println("Control Key Pressed" );
        //         waitABit(1000);
        //         robot.keyPress(java.awt.event.KeyEvent.VK_TAB);   
        //         System.out.println("TAB Key Pressed" );
        //         waitABit(1000);
        //         robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
        //         System.out.println("Control Key Released" );
        //         robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);   
        //         System.out.println("TAB Key Released" );
        //         
        //         
        //         //Navigate from Left to Right
        //         Actions action= new Actions(driver);
        //         action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

        //
        //         System.out.println("Switching Tabs" );
        //         robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
        //         System.out.println("Control Key Pressed 2" );
        //         robot.keyPress(java.awt.event.KeyEvent.VK_TAB);   
        //         System.out.println("TAB Key Pressed2 " );
        //         robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
        //         System.out.println("Control Key Released 2" );
        //         robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);   
        //         System.out.println("TAB Key Released 2" );

        //         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_CONTROL);
        //         System.out.println("Control Key Released" );
        //         robot.keyRelease(java.awt.event.KeyEvent.VK_W);   
        //         System.out.println("W Key Released" );
    } catch (AWTException e) {
        System.out.println("Error has occured when attempting to Switch Browser Tabs!!");
        e.printStackTrace();
    }
}

From source file:com.chtr.tmoauto.webui.CommonFunctions.java

@Override
public void typeUsingRobot(String locator, String value) {
    WebElement we = findElement(locator);
    // try to click otherwise ignore if it fails
    try {/*from   w ww  .j  a v  a  2s  . co m*/
        we.click();
    } catch (Exception e) {
    }
    ClipboardOwner clipboardOwner = new ClipboardOwner() {
        @Override
        public void lostOwnership(Clipboard clipboard, Transferable contents) {
        }
    };
    Robot robot;
    try {
        robot = new Robot();
        try {
            we.sendKeys(value);
        } catch (Exception e) {
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            StringSelection stringSelection = new StringSelection(value);
            clipboard.setContents(stringSelection, clipboardOwner);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        }
    } catch (AWTException e1) {
        e1.printStackTrace();
    }
}

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

/**
 * Do type./*  w w w  . j av  a  2 s  . c  om*/
 * 
 * @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

/**
 * Get the selected text in webpage to the clipboard and compare the value
 * with the given input./*from  ww w  . j  av a2 s .c  o  m*/
 * 
 * @param value
 *            the value
 * @throws Exception
 *             the exception
 */

private void fireEventVerifyValue(final String value) throws Exception {

    String clipBoardText = "";
    Robot robot = new Robot();

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

    Utils.pause(retryInterval);
    Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

    try {
        if (trans != null) {
            clipBoardText = (String) trans.getTransferData(DataFlavor.stringFlavor);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (clipBoardText.equals(value)) {

        reportResults(ReportLogger.ReportLevel.SUCCESS, "Fire Event", "Success",
                "Verify value passed. Value : " + value);

    } else {

        reportResults(true, ReportLogger.ReportLevel.FAILURE, "Fire Event", "Error",
                "Verify value match expected. ::: " + "Expected value : " + value + " Actual value : "
                        + clipBoardText);
    }
}

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

/**
 * Performs a Java robot click on the specific coordinates. <br>
 * //from ww  w .ja v  a 2 s .  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:com.virtusa.isq.rft.runtime.RFTCommandBase.java

/**
 * Fires a set of java robot key events into the webpage.
 * /* ww  w  .j a  va2s .c o  m*/
 * @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:com.virtusa.isq.rft.runtime.RFTCommandBase.java

/**
 * Fires a set of java robot mouse events into the webpage.
 * /*from w  w  w .  j  a  v  a 2  s .co 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:com.photon.phresco.Screens.InvalidJarBase.java

public void instantiateBrowser(String selectedBrowser, String selectedPlatform, String applicationURL,
        String applicationContext) throws ScreenException, MalformedURLException {

    if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_CHROME)) {
        try {//from w w  w. jav a 2  s. c om
            // "D:/Selenium-jar/chromedriver_win_19.0.1068.0/chromedriver.exe"
            chromeService = new ChromeDriverService.Builder()
                    .usingDriverExecutable(new File(getChromeLocation())).usingAnyFreePort().build();

            log.info("-------------***LAUNCHING GOOGLECHROME***--------------");

            driver = new ChromeDriver(chromeService);
            //driver.manage().window().maximize();
            //   driver = new ChromeDriver(chromeService, chromeOption);
            // driver.manage().timeouts().implicitlyWait(30,
            // TimeUnit.SECONDS);            
            //driver.navigate().to(applicationURL + applicationContext);
            driver.manage().window().maximize();
            driver.navigate().to(applicationURL + applicationContext);

        } catch (Exception e) {
            e.printStackTrace();
        }

    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_IE)) {
        log.info("---------------***LAUNCHING INTERNET EXPLORE***-----------");
        driver = new InternetExplorerDriver();
        driver.manage().window().maximize();
        driver.navigate().to(applicationURL + applicationContext);

    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_FIREFOX)) {
        log.info("-------------***LAUNCHING FIREFOX***--------------");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to(applicationURL + applicationContext);

    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_OPERA)) {
        log.info("-------------***LAUNCHING OPERA***--------------");
        System.out.println("******entering window maximize********");
        try {
            System.out.println("******entering window maximize********");
            Robot robot;
            try {
                robot = new Robot();
                robot.keyPress(KeyEvent.VK_ALT);
                robot.keyPress(KeyEvent.VK_SPACE);
                robot.keyRelease(KeyEvent.VK_ALT);
                robot.keyRelease(KeyEvent.VK_SPACE);
                robot.keyPress(KeyEvent.VK_X);
                robot.keyRelease(KeyEvent.VK_X);
            } catch (AWTException e) {

                e.printStackTrace();
            }

        } catch (Exception e) {

            e.printStackTrace();
        }

    } else {
        throw new ScreenException("------Only FireFox,InternetExplore and Chrome works-----------");
    }

}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

/**
 * Do type./*from w  ww  .j  a v a2  s .com*/
 * 
 * @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;
    }
    Robot robot = getRobot();
    try {
        robot.keyPress(keyCodes[offset]);
        doTypeKeys(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}