Java Code Examples for java.awt.Robot#keyPress()

The following examples show how to use java.awt.Robot#keyPress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JapaneseReadingAttributes.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void enterInput(Robot robotKeyInput,
        ArrayList<Integer> keyInputs) {
    textFieldReading.setText("");
    textFieldMain.setText("");

    String strKeyInput = "KeyPress=>";
    int nOfKeyInputs = keyInputs.size();
    for (int i = 0; i < nOfKeyInputs; i++) {
        int keyToUse = keyInputs.get(i);
        robotKeyInput.keyPress(keyToUse);
        robotKeyInput.keyRelease(keyToUse);
        strKeyInput += (Integer.toHexString(keyToUse)) + ":";
    }

    System.out.println(strKeyInput);
}
 
Example 2
Source File: DeadKeySystemAssertionDialog.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
        Frame frame = new Frame();
        frame.setSize(300, 200);

        TextField textField = new TextField();
        frame.add(textField);

        frame.setVisible(true);
        toolkit.realSync();

        textField.requestFocus();
        toolkit.realSync();

        // Check that the system assertion dialog does not block Java
        Robot robot = new Robot();
        robot.setAutoDelay(50);
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);
        toolkit.realSync();

        frame.setVisible(false);
        frame.dispose();
    }
 
Example 3
Source File: MultiResolutionSplashTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void testFocus() throws Exception {

        Robot robot = new Robot();
        robot.setAutoDelay(50);

        Frame frame = new Frame();
        frame.setSize(100, 100);
        String test = "123";
        TextField textField = new TextField(test);
        textField.selectAll();
        frame.add(textField);
        frame.setVisible(true);
        robot.waitForIdle();

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

        frame.dispose();

        if (!textField.getText().equals("ab")) {
            throw new RuntimeException("Focus is lost!");
        }
    }
 
Example 4
Source File: OpenNewTabChromeTest.java    From webdrivermanager-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    // Open URL in default tab
    driver.get("https://wikipedia.org/");

    // If Mac OS X, the key combination is CMD+t, otherwise is CONTROL+t
    int vkControl = IS_OS_MAC ? VK_META : VK_CONTROL;
    Robot robot = new Robot();
    robot.keyPress(vkControl);
    robot.keyPress(VK_T);
    robot.keyRelease(vkControl);
    robot.keyRelease(VK_T);

    // Wait up to 5 seconds to the second tab to be opened
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(numberOfWindowsToBe(2));

    // Switch to new tab
    List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
    System.err.println(windowHandles);
    driver.switchTo().window(windowHandles.get(1));

    // Open other URL in second tab
    driver.get("https://google.com/");
}
 
Example 5
Source File: DeadKeySystemAssertionDialog.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
        Frame frame = new Frame();
        frame.setSize(300, 200);

        TextField textField = new TextField();
        frame.add(textField);

        frame.setVisible(true);
        toolkit.realSync();

        textField.requestFocus();
        toolkit.realSync();

        // Check that the system assertion dialog does not block Java
        Robot robot = new Robot();
        robot.setAutoDelay(50);
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);
        toolkit.realSync();

        frame.setVisible(false);
        frame.dispose();
    }
 
Example 6
Source File: bug8002077.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
    Robot robot = new Robot();
    robot.setAutoDelay(50);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            fileChooserState = new JFileChooser().showSaveDialog(null);
        }
    });
    toolkit.realSync();

    Util.hitMnemonics(robot, KeyEvent.VK_N);
    toolkit.realSync();

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

    Util.hitMnemonics(robot, KeyEvent.VK_S);
    toolkit.realSync();

    if (fileChooserState != JFileChooser.APPROVE_OPTION) {
        throw new RuntimeException("Save button is not pressed!");
    }
}
 
Example 7
Source File: KeyCharTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();

        Frame frame = new Frame();
        frame.setSize(300, 300);
        frame.setVisible(true);
        toolkit.realSync();

        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_DELETE);
        robot.keyRelease(KeyEvent.VK_DELETE);
        toolkit.realSync();

        frame.dispose();

        if (eventsCount != 3) {
            throw new RuntimeException("Wrong number of key events: " + eventsCount);
        }
    }
 
Example 8
Source File: KeyCharTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();

        Frame frame = new Frame();
        frame.setSize(300, 300);
        frame.setVisible(true);
        toolkit.realSync();

        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_DELETE);
        robot.keyRelease(KeyEvent.VK_DELETE);
        toolkit.realSync();

        frame.dispose();

        if (eventsCount != 3) {
            throw new RuntimeException("Wrong number of key events: " + eventsCount);
        }
    }
 
Example 9
Source File: bug8057893.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    robot.setAutoDelay(50);
    SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();

    EventQueue.invokeAndWait(() -> {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JComboBox<String> comboBox = new JComboBox<>(new String[]{"one", "two"});
        comboBox.setEditable(true);
        comboBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if ("comboBoxEdited".equals(e.getActionCommand())) {
                    isComboBoxEdited = true;
                }
            }
        });
        frame.add(comboBox);
        frame.pack();
        frame.setVisible(true);
        comboBox.requestFocusInWindow();
    });

    toolkit.realSync();

    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    toolkit.realSync();

    if(!isComboBoxEdited){
        throw new RuntimeException("ComboBoxEdited event is not fired!");
    }
}
 
Example 10
Source File: NativeEventsTest.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public void enteredGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_ENTERED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.keyPress(KeyEvent.VK_ALT);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    r.keyRelease(KeyEvent.VK_ALT);
    new EventQueueWait() {
        @Override
        public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    new Actions(driver).moveToElement(t).keyDown(Keys.ALT).moveToElement(b).click().keyUp(Keys.ALT).perform();
    AssertJUnit.assertEquals(expected, t.getText());

}
 
Example 11
Source File: NativeEventsTest.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public void exitedGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_EXITED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.keyPress(KeyEvent.VK_ALT);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(button, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    r.keyRelease(KeyEvent.VK_ALT);
    new EventQueueWait() {
        @Override
        public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");

    String expected = t.getText();
    tclear();
    new Actions(driver).moveToElement(t).keyDown(Keys.ALT).moveToElement(b).moveToElement(t).keyUp(Keys.ALT).perform();
    AssertJUnit.assertEquals(expected, t.getText());
}
 
Example 12
Source File: ConsumedKeyTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final int key) throws Exception {
    passed = false;
    try {
        SwingUtilities.invokeAndWait(() -> {
            frame = new JFrame();
            JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
            JPanel panel = new JPanel();
            panel.add(combo);
            combo.requestFocusInWindow();
            frame.setBounds(100, 150, 300, 100);
            addAction(panel, key);
            frame.add(panel);
            frame.setVisible(true);
        });

        Robot robot = new Robot();
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyPress(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyRelease(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        if (!passed) {
            throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
        }
    } finally {
        if (frame != null) {
            frame.dispose();
        }
    }

}
 
Example 13
Source File: ConsumedKeyTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final int key) throws Exception {
    passed = false;
    try {
        SwingUtilities.invokeAndWait(() -> {
            frame = new JFrame();
            JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"});
            JPanel panel = new JPanel();
            panel.add(combo);
            combo.requestFocusInWindow();
            frame.setBounds(100, 150, 300, 100);
            addAction(panel, key);
            frame.add(panel);
            frame.setVisible(true);
        });

        Robot robot = new Robot();
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyPress(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        robot.keyRelease(key);
        robot.waitForIdle();
        ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
        if (!passed) {
            throw new RuntimeException("FAILED: " + KeyEvent.getKeyText(key) + " was consumed by combo box");
        }
    } finally {
        if (frame != null) {
            frame.dispose();
        }
    }

}
 
Example 14
Source File: AwtKeyboard.java    From phoenix.webui.framework with Apache License 2.0 5 votes vote down vote up
@Override
public void enter()
{
	try
	{
		Robot robot = new Robot();
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
	}
	catch (AWTException e)
	{
		e.printStackTrace();
	}
}
 
Example 15
Source File: GoToLine.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void goToLine(int line,Robot robot) {
    GoToLine op=new GoToLine();
    java.awt.Point p;
    int x,y;
    
    String s=String.valueOf(line);
    int c;
    robot.waitForIdle();
    robot.delay(200);
    for (int i=0;i < s.length();i++) {
        c=(int)s.charAt(i);
        robot.keyPress(c);
        robot.delay(50);
        robot.keyRelease(c);
    }
    p=op.btGoto().getLocationOnScreen();
    x=p.x+op.btGoto().getWidth()/2;
    y=p.y+op.btGoto().getHeight()/2;
    robot.mouseMove(x,y);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(50);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(50);
    robot.waitForIdle();
    while (op.isVisible()) {
        robot.delay(50);
    }
}
 
Example 16
Source File: bug8021253.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
        Robot robot = new Robot();
        robot.setAutoDelay(50);

        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });

        toolkit.realSync();

        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                fileChooser.setSelectedFile(file);
            }
        });

        toolkit.realSync();

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        toolkit.realSync();

        if (!defaultKeyPressed) {
            throw new RuntimeException("Default button is not pressed");
        }
    }
 
Example 17
Source File: MultiResolutionSplashTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testFocus() throws Exception {

        System.out.println("Focus Test!");
        Robot robot = new Robot();
        robot.setAutoDelay(50);

        Frame frame = new Frame();
        frame.setSize(100, 100);
        String test = "123";
        TextField textField = new TextField(test);
        textField.selectAll();
        frame.add(textField);
        frame.setVisible(true);
        robot.waitForIdle();

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

        frame.dispose();

        if(!textField.getText().equals("ab")){
            throw new RuntimeException("Focus is lost!");
        }
    }
 
Example 18
Source File: bug8072767.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    robot.setAutoDelay(50);
    SwingUtilities.invokeAndWait(bug8072767::createAndShowGUI);
    robot.waitForIdle();
    SwingUtilities.invokeAndWait(() -> {
        point = table.getLocationOnScreen();
        Rectangle rect = table.getCellRect(0, 0, true);
        point.translate(rect.width / 2, rect.height / 2);
    });
    robot.mouseMove(point.x, point.y);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.waitForIdle();

    robot.keyPress(KeyEvent.VK_1);
    robot.keyRelease(KeyEvent.VK_1);
    robot.waitForIdle();

    SwingUtilities.invokeAndWait(() -> {
        point = frame.getLocationOnScreen();
        point.translate(frame.getWidth() / 2, frame.getHeight() / 2);
    });

    robot.mouseMove(point.x, point.y);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.waitForIdle();

    SwingUtilities.invokeAndWait(() -> {
        testPass = TEST2.equals(table.getValueAt(0, 0));
        frame.dispose();
    });

    if (!testPass) {
        throw new RuntimeException("Table cell is not edited!");
    }
}
 
Example 19
Source File: NativeEventsTest.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private void checkKeyEvent(int eventToCheck, String keysToSend, int... keysToPress)
        throws InterruptedException, InvocationTargetException, AWTException {
    events = eventToCheck;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("enter-text"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(textField, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(textField, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    for (int keysToPres : keysToPress) {
        r.keyPress(keysToPres);
    }
    for (int i = keysToPress.length - 1; i >= 0; i--) {
        r.keyRelease(keysToPress[i]);
    }
    new EventQueueWait() {
        @Override
        public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    b.sendKeys(keysToSend);
    System.out.println("Expected: " + expected);
    AssertJUnit.assertEquals(expected, t.getText());

    new Actions(driver).moveToElement(b).click().perform();
    AssertJUnit.assertEquals(expected, t.getText());
}
 
Example 20
Source File: NativeEventsTest.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private void checkAltRightClickEvent(int eventToCheck) throws InterruptedException, InvocationTargetException, AWTException {
    events = eventToCheck;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.keyPress(KeyEvent.VK_ALT);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON3_MASK);
    r.mouseRelease(InputEvent.BUTTON3_MASK);
    r.keyRelease(KeyEvent.VK_ALT);
    new EventQueueWait() {
        @Override
        public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(button, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    new Actions(driver).moveToElement(b).keyDown(Keys.ALT).contextClick().keyUp(Keys.ALT).perform();
    AssertJUnit.assertEquals(expected, t.getText());
    // Wait till the previous click is processed by the EDT
    Thread.sleep(500);
}