Example usage for java.awt Robot keyRelease

List of usage examples for java.awt Robot keyRelease

Introduction

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

Prototype

public synchronized void keyRelease(int keycode) 

Source Link

Document

Releases a given key.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_A);
    robot.waitForIdle();/*from  ww w.  j  a  v a  2  s.co  m*/
    robot.keyRelease(KeyEvent.VK_A);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.delay(40);// ww w  .ja va2  s  . co  m
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.delay(404);

    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor[] flavors = cb.getAvailableDataFlavors();
    for (DataFlavor flavor : flavors) {
        if (flavor.toString().indexOf("java.awt.Image") <= 0) {
            continue;
        }
        Image i = (Image) cb.getData(flavor);
        BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(i, 0, 0, null);
        g.dispose();
        ImageIO.write(bi, "png", new File("c:/Java_Dev/test.png"));
    }
}

From source file:RobotTest.java

/**
 * Runs a sample test procedure/*from   w  w  w  . j  av a  2s.c  om*/
 * 
 * @param robot
 *          the robot attached to the screen device
 */
public static void runTest(Robot robot) {
    // simulate a space bar press
    robot.keyPress(' ');
    robot.keyRelease(' ');

    // simulate a tab key followed by a space
    robot.delay(2000);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(' ');
    robot.keyRelease(' ');

    // simulate a mouse click over the rightmost button
    robot.delay(2000);
    robot.mouseMove(200, 50);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);

    // capture the screen and show the resulting image
    robot.delay(2000);
    BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300));

    ImageFrame frame = new ImageFrame(image);
    frame.setVisible(true);
}

From source file:RobotUtilities.java

public static void sendKeysCombo(String keys[]) {
    try {//from   w w  w .  j av  a2s .  c  o  m

        Robot robot = new Robot();

        Class<?> cl = KeyEvent.class;

        int[] intKeys = new int[keys.length];

        for (int i = 0; i < keys.length; i++) {
            Field field = cl.getDeclaredField(keys[i]);
            intKeys[i] = field.getInt(field);
            robot.keyPress(intKeys[i]);
        }

        for (int i = keys.length - 1; i >= 0; i--)
            robot.keyRelease(intKeys[i]);
    } catch (Throwable e) {
        System.err.println(e);
    }
}

From source file:KeyboardEmulator.java

public static String type(String s, boolean appendEnter) throws AWTException {
    if (isBlank(s))
        return s;

    String typed = "";
    try {//from   www  .ja va  2 s.c o  m
        Robot keyboard = getKeyboard();
        int size = s.length();
        char c;
        for (int i = 0; i < size; i++) {
            c = s.charAt(i);
            int idx = shiftChars.indexOf(c);
            if (idx > -1) {
                char cc = unShiftChars.charAt(idx);
                keyboard.keyPress(Shift);
                keyboard.keyPress(KeyEvent.getExtendedKeyCodeForChar(cc));
                keyboard.keyRelease(KeyEvent.getExtendedKeyCodeForChar(cc));
                keyboard.keyRelease(Shift);
            } else {
                keyboard.keyPress(KeyEvent.getExtendedKeyCodeForChar(c));
                keyboard.keyRelease(KeyEvent.getExtendedKeyCodeForChar(c));
            }
            typed += String.valueOf(c);
        }
        if (appendEnter) {
            keyboard.keyPress(Enter);
            keyboard.keyRelease(Enter);
        }
    } catch (AWTException e) {

    } finally {
        return typed;
    }
}

From source file:net.sourceforge.docfetcher.util.AppUtil.java

public static void sendHotkeyToFront() {
    try {//from w  ww .  j  av  a  2  s .c  o m
        int one = SettingsConf.IntArray.HotkeyToFront.get()[0];
        one = KeyCodeTranslator.translateSWTKey(one);
        int two = SettingsConf.IntArray.HotkeyToFront.get()[1];
        two = KeyCodeTranslator.translateSWTKey(two);
        Robot robot = new Robot();
        robot.keyPress(one);
        robot.keyPress(two);
        robot.delay(500);
        robot.keyRelease(two);
        robot.keyRelease(one);
    } catch (AWTException e) {
        AppUtil.showStackTrace(e);
    }
}

From source file:com.seleniumtests.driver.CustomEventFiringWebDriver.java

/**
 * send keys to desktop/*from w ww .j  ava2s  .  co m*/
 * This is useful for typing special keys like ENTER
 * @param keys
 */
public static void sendKeysToDesktop(List<Integer> keyCodes, DriverMode driverMode,
        SeleniumGridConnector gridConnector) {
    if (driverMode == DriverMode.LOCAL) {
        try {
            Robot robot = new Robot();

            WaitHelper.waitForSeconds(1);

            for (Integer key : keyCodes) {
                robot.keyPress(key);
                robot.keyRelease(key);
            }
        } catch (AWTException e) {
            throw new ScenarioException("could not initialize robot to type keys: " + e.getMessage());
        }
    } else if (driverMode == DriverMode.GRID && gridConnector != null) {
        gridConnector.sendKeysWithKeyboard(keyCodes);
    } else {
        throw new ScenarioException("driver supports sendKeysToDesktop only in local and grid mode");
    }
}

From source file:org.suren.autotest.web.framework.awt.AwtKeyboard.java

@Override
public void enter() {
    try {/*  w ww  . j  a v  a 2 s  . c  o m*/
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } catch (AWTException e) {
        e.printStackTrace();
    }
}

From source file:org.suren.autotest.web.framework.awt.AwtKeyboard.java

@Override
public void space() {
    try {/*w  w w  . j a  va2 s. c om*/
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
    } catch (AWTException e) {
        e.printStackTrace();
    }
}