3

Hey there using this code snippet:

try {

      Robot robot = new Robot();

      robot.keyPress(KeyEvent.VK_H);
      robot.keyRelease(KeyEvent.VK_H);
      robot.keyPress(KeyEvent.VK_A);
      robot.keyRelease(KeyEvent.VK_A);
      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);

 } catch (AWTException e) {
      e.printStackTrace();
 }

i get this result :

hallo

But is there any way to shorten this process ? e.g. something like:

try {

       Robot robot = new Robot();

       String  word = "hallo";

       // something like:
       robot.keyPress(KeyEvent.word);

   } catch (AWTException e) {
       e.printStackTrace();
   }

I know this example doesn't work, but I couldn't find any documentation about this.

You have any ideas? greetings and thanks

Alex Wittig
  • 2,800
  • 1
  • 33
  • 42
user3297073
  • 129
  • 1
  • 12
  • you could create a method that takes a char input, figures out what letter was passed and then types given char. It would be more code than this, but it will be less hardcoding. See [this](http://stackoverflow.com/questions/1248510/convert-string-to-keyevents) for an example. – turbo Feb 11 '14 at 14:59
  • The answer to your question is [here][1]. [1]: http://stackoverflow.com/questions/15260282/converting-a-char-into-java-keyevent-keycode The method is provided. – GreySwordz Feb 11 '14 at 15:07

2 Answers2

4

If you're on Java 7 then you can use the KeyEvent.getExtendedKeyCodeForChar method to get the key code from a char:

   import java.awt.event.KeyEvent; 

   [...]

   public static void type(Robot robot, String word) {
        for (int i = 0; i < word.length(); i++) {
            int keyCode = KeyEvent.getExtendedKeyCodeForChar(word.charAt(i));
            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);
        }
    }

   [...]

   Robot robot = new Robot();
   type(robot, "hallo");
Nigel Tufnel
  • 11,146
  • 4
  • 35
  • 31
  • ok thx but what do i have to import for "getExtendedKeyCodeForChar", otherwise this method is undefined for the type keyevent :/ ? – user3297073 Feb 11 '14 at 15:23
  • `import java.awt.event.KeyEvent` would suffice: `getExtendedKeyCodeForChar` is a static method in the class `KeyEvent`. If you're getting "method is not defined" error then I guess you're using Java 6 or older and you should go with another answer. – Nigel Tufnel Feb 11 '14 at 15:34
  • so i should use java 7 yea? – user3297073 Feb 11 '14 at 15:37
  • If you can then by all means do upgrade to Java 7. – Nigel Tufnel Feb 11 '14 at 15:39
  • yea i installed, but i cannot change the "execution environment" from my project(the latest is still "Java Se 6"), you know why? (using eclipse) – user3297073 Feb 11 '14 at 15:42
1

Make a method like this. Simply use the methods that take a character.

public void press(String s, Robot r)
{
    for (char ch : s.toCharArray())
    {
        if (Character.isUpperCase(ch))
            r.keyPress(KeyEvent.VK_SHIFT);

        r.keyPress(Character.toUpperCase(ch));
        r.keyRelease(Character.toUpperCase(ch));
    }
}

Or you can use this to get the KeyCode.

KeyStroke.getKeyStroke(ch, 0).getKeyCode();

Or also

KeyEvent.getExtendedKeyCodeForChar(ch);

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91