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: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  w w w .  j a v  a  2s . 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);//from  w ww.  jav a 2s  . c  o  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:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();

    robot.delay(3000);//  www.java 2  s. c  o m

    robot.keyPress(KeyEvent.VK_Q);
    robot.keyPress(KeyEvent.VK_W);
    robot.keyPress(KeyEvent.VK_E);
    robot.keyPress(KeyEvent.VK_R);
    robot.keyPress(KeyEvent.VK_T);
    robot.keyPress(KeyEvent.VK_Y);
}

From source file:Main.java

/**
 * Press key.//from  w ww  . java 2  s. c o  m
 *
 * @param key
 *            the key
 */
public static void pressKey(final int key) {
    try {
        final Robot robot = new Robot();
        robot.keyPress(key);
    } catch (final AWTException e) {
        // ExceptionUtil.handle(e);
    }
}

From source file:RobotTest.java

/**
 * Runs a sample test procedure//from   w w  w  .j a v  a2s  . c  o  m
 * 
 * @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 {/* www . j a v  a2 s.co  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 {/*  w w  w.  ja v  a 2s .c om*/
        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  w  w  . j  a v  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   www . j  av a 2 s. 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");
    }
}