Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type a String using java.awt.Robot

People also ask

How does Java Robot work?

The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue.

How do you use special characters in Robot class?

Just type a text string in the top text field and click the button. Note it only supports a few special characters. But note this is not a reliable solution because the mapping depends on the keyboard.

How do you send values in Robot class?

findElement(By. xpath("//xpath of the element")). sendKeys("")// id or class can be used as locators too. 2- Then use 'Robot class' to send values to the field (using CAPSLOCK or SHIFT keys for changing the letters to uppercase).

What is Java AWT Robot?

The Robot class in the Java AWT package is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.


Common solution is to use the clipboard:

String text = "Hello World";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

Since Java 7 you can also use KeyEvent.getExtendedKeyCodeForChar(c). An example for lower case only could be:

void sendKeys(Robot robot, String keys) {
    for (char c : keys.toCharArray()) {
        int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
        if (KeyEvent.CHAR_UNDEFINED == keyCode) {
            throw new RuntimeException(
                "Key code not found for character '" + c + "'");
        }
        robot.keyPress(keyCode);
        robot.delay(100);
        robot.keyRelease(keyCode);
        robot.delay(100);
    }
}

You need to "type" the character, which is a press AND release action...

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

Now you could just copy and paste it three times, but I'd just put it in a loop


You can enter value in a string and then you can use that string as explained by Eng.Fouad. But there is no fun in using it, you can give a try to this

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);

and you can also use Thread.sleep so that it can enter data bit slowly.