0

Hi i was wondering if there is a way i can make an auto typer by using the robot class? Is there a way i could make a string into char[] and type out each letter? anyway i could use a case for each letter (Example below).

        case ' ': key = "VK_SPACE"; shiftOn = false; break;
        case 'a': key = "VK_A"; shiftOn = false;    break;
        case 'b': key = "VK_B"; shiftOn = false;    break;
        case 'c': key = "VK_C"; shiftOn = false;    break;
        case 'd': key = "VK_D"; shiftOn = false;    break;
        case 'e': key = "VK_E"; shiftOn = false;    break;
        case 'f': key = "VK_F"; shiftOn = false;    break;
        case 'g': key = "VK_G"; shiftOn = false;    break;
        case 'h': key = "VK_H"; shiftOn = false;    break;
        case 'i': key = "VK_I"; shiftOn = false;    break;
        case 'j': key = "VK_J"; shiftOn = false;    break;
        case 'k': key = "VK_K"; shiftOn = false;    break;
        case 'l': key = "VK_L"; shiftOn = false;    break;

etc...

WillBaker
  • 33
  • 1
  • 6
  • Convert a String into char[] with `toCharArray`: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toCharArray() – Jeroen Ingelbrecht Aug 28 '13 at 21:44
  • Yes, and its messy. Check out [this example](http://stackoverflow.com/questions/14572270/how-can-i-perfectly-simulate-keyevents/14615814#14615814). This uses two different methods. One is direct injection into the event queue, the other issuing the robot, but it uses the same process to generate the key sequences – MadProgrammer Aug 28 '13 at 21:45
  • How about `static char[] getCharArray(String stringName) { return stringName.toCharArray(); }` To convert string to char[]? – WillBaker Aug 28 '13 at 21:47
  • Here's a [similar question](http://stackoverflow.com/questions/15260282/converting-a-char-into-java-keyevent-keycode) with a simple solution. – sgbj Aug 28 '13 at 22:01

2 Answers2

0

With a little help from the code here:

    Thread.sleep(2000); // Give me time to open up notepad
    Robot r = new Robot();
    for (char c : "I like playing with fire, and Java.".toCharArray()) {
        int code = KeyEvent.getExtendedKeyCodeForChar(c);
        if (Character.isUpperCase(c))
            r.keyPress(KeyEvent.VK_SHIFT);
        r.keyPress(code);
        r.keyRelease(code);
        if (Character.isUpperCase(c))
            r.keyRelease(KeyEvent.VK_SHIFT);
    }

You'll have to do a little more work to get characters like !@#$%&*()_+ to work.

Community
  • 1
  • 1
sgbj
  • 2,264
  • 17
  • 14
0

If you're working with several keyboard layouts, the solution is to paste each character with some clipboard action and Ctrl + V key presses; or else, you may get buggy results such as not being able to type in slashes in Brazilian keyboards (a problem I had a while ago). If not, you can use sbat's solution. Mine is just a workaround the fact that key codes aren't the same in all keyboards, but it works slower because of that (I think).

public static void paste(String text) throws Exception {
    Robot rob = new Robot(); //Robot for typing
    Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); //Clipboard
    old = (String) c.getData(DataFlavor.stringFlavor); //Clipboard contents before function
    for (int i = 0; i < text.length(); i++) {
        c.setContents(new StringSelection("" + text.charAt(i)), null); //Set clipboard

        //Ctrl + V
        rob.keyPress(KeyEvent.VK_CONTROL); 
            rob.keyPress(KeyEvent.VK_V);
            rob.keyRelease(KeyEvent.VK_V);
        rob.keyRelease(KeyEvent.VK_CONTROL);
    }
    c.setContents(new StringSelection(old), null); //Restore old clipboard contents
}
borges
  • 371
  • 4
  • 12