Type a String using java.awt.Robot

35,354

Solution 1

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);

Solution 2

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);
    }
}

Solution 3

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

Solution 4

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.

Solution 5

This doesn't type the entire "string" but helps to type whatever you want other than one character at a time.

    Runtime.getRuntime().exec("notepad.exe");//or anywhere you want.
   Thread.sleep(5000);//not required though gives a good feel.
    Robot r=new Robot();      
     String a="Hi My name is Whatever you want to say.";
    char c;
     int d=a.length(),e=0,f=0;

     while(e<=d)
    {
     c=a.charAt(e);
     f=(int) c; //converts character to Unicode. 
      r.keyPress(KeyEvent.getExtendedKeyCodeForChar(f));
     e++;
       Thread.sleep(150);

       }

see it works perfectly and it's awesome! Though it doesn't work for special characters which cannot be traced by unicode like |,!...etc.

Share:
35,354
Radiodef
Author by

Radiodef

I'm really a musician, I guess, but lately I've been thinking about pursuing programming as a career instead. I took a few programming classes in high school, but otherwise I've just studied it as a hobby off and on. I know Java the best, but I also know some C++, JavaScript and Objective-C. The most substantial program I've written is Epsilon, a text-based calculator, written in Java. Block Breaker (a Breakout clone) is also cool. Along with computer programming, I've also studied math, philosophy, psychology and Japanese online (all to varying degrees), so I care a great deal about the quality of educational resources available on the internet. Below are some answers of mine which I'm particularly fond of, either because I think they're useful or just because they took a lot of work: How do I use audio sample data from Java Sound? Second order generics seem to behave differently than first order generics (explanation of Java capture conversion) Consumer&lt;T&gt; mapped Class&lt;T&gt; in HashMap Where is the Java Swing counterpart of “GetMessage()” loop? How to get annotation from overridden method in Java? (method override testing with reflection) I think all of my most upvoted answers are fine, but upvotes are really just an indication of the question's popularity, so they don't always correspond to what's the most interesting or challenging. If I've left a negative comment on your answer, please just address the issue and notify me when you're done. When I think issues that I've pointed out have been adequately addressed, I generally delete my comments so it looks like the conversation never took place. I believe this is how most criticisms should happen on the site. Just fix the problem and move on.

Updated on November 14, 2020

Comments

  • Radiodef
    Radiodef over 3 years

    I already know how to use java.awt.Robot to type a single character using keyPress, as seen below. How can I simply enter a whole pre-defined String value at once into a textbox?

    robot.keyPress(KeyEvent.VK_1);
    robot.keyPress(KeyEvent.VK_1);
    robot.keyPress(KeyEvent.VK_1);  
    // instead, enter String x = "111"
    
  • Admin
    Admin about 9 years
    111 is just an example. Say I want it to enter "Hello world"!
  • jansohn
    jansohn about 7 years
    Thanks. Upper-case can be easily checked with Character.isUpperCase(c).
  • ed22
    ed22 almost 5 years
    How would I handle upper case here? Press a VK_SHIFT somewhere?
  • Shoaib Akhtar
    Shoaib Akhtar over 4 years
    Can you help me on this? I want to send a string which has caps, small and special character.. stackoverflow.com/questions/58220899/…