Keypress Generator

Here is a quick and dirty way to simulate a keypress using the Robot class in Java. If you provide a string to the constructor and call the type() method, each of the characters in the string will be generated as a keypress. The code only supports A-Z and a-z, but could be modified to cover a larger character set.

import java.awt.Robot;
import java.awt.event.KeyEvent;


public class Typer {
  private Robot robot = null;
  private String characters = null;

  public Typer(String characters) {
    if((characters == null) || ("").equals(characters.trim())) {
      characters = "";
    }
    this.characters = characters;

    try {
      robot = new Robot();
    } catch(Exception e) {
    }
    return;
  }

  public String getCharacters() {
    return characters;
  }

  public boolean type() {
    if(robot == null) {
      return false;
    }

    for(int i = 0; i < this.getCharacters().length(); i++) {
      robot.keyPress(typeLetter(this.getCharacters().charAt(i)));
    }
    robot.keyPress(KeyEvent.VK_ENTER);
    return true;
  }

  protected boolean delay(int delay) {
    if(this.robot != null) {
      robot.delay(delay);
      return true;
    }
    return false;
  }

  private int typeLetter(char c) {
    int keyEvent = 0;

    try {
      switch(c) {
      case 'a':
      case 'A':
        keyEvent = KeyEvent.VK_A;
        break;
      case 'b':
      case 'B':
        keyEvent = KeyEvent.VK_B;
        break;
      case 'c':
      case 'C':
        keyEvent = KeyEvent.VK_C;
        break;
      case 'd':
      case 'D':
        keyEvent = KeyEvent.VK_D;
        break;
      case 'e':
      case 'E':
        keyEvent = KeyEvent.VK_E;
        break;
      case 'f':
      case 'F':
        keyEvent = KeyEvent.VK_F;
        break;
      case 'g':
      case 'G':
        keyEvent = KeyEvent.VK_G;
        break;
      case 'h':
      case 'H':
        keyEvent = KeyEvent.VK_H;
        break;
      case 'i':
      case 'I':
        keyEvent = KeyEvent.VK_I;
        break;
      case 'j':
      case 'J':
        keyEvent = KeyEvent.VK_J;
        break;
      case 'k':
      case 'K':
        keyEvent = KeyEvent.VK_K;
        break;
      case 'l':
      case 'L':
        keyEvent = KeyEvent.VK_L;
        break;
      case 'm':
      case 'M':
        keyEvent = KeyEvent.VK_M;
        break;
      case 'n':
      case 'N':
        keyEvent = KeyEvent.VK_N;
        break;
      case 'o':
      case 'O':
        keyEvent = KeyEvent.VK_O;
        break;
      case 'p':
      case 'P':
        keyEvent = KeyEvent.VK_P;
        break;
      case 'q':
      case 'Q':
        keyEvent = KeyEvent.VK_Q;
        break;
      case 'r':
      case 'R':
        keyEvent = KeyEvent.VK_R;
        break;
      case 's':
      case 'S':
        keyEvent = KeyEvent.VK_S;
        break;
      case 't':
      case 'T':
        keyEvent = KeyEvent.VK_T;
        break;
      case 'u':
      case 'U':
        keyEvent = KeyEvent.VK_U;
        break;
      case 'v':
      case 'V':
        keyEvent = KeyEvent.VK_V;
        break;
      case 'w':
      case 'W':
        keyEvent = KeyEvent.VK_W;
        break;
      case 'x':
      case 'X':
        keyEvent = KeyEvent.VK_X;
        break;
      case 'y':
      case 'Y':
        keyEvent = KeyEvent.VK_Y;
        break;
      case 'z':
      case 'Z':
        keyEvent = KeyEvent.VK_Z;
        break;
      }
    } catch(Exception e) {
    }
    return keyEvent;
  }
}

Generate All Permutations of a String in Java

This is a recursive function to generate all permutations of a string in Java. For example, if “abc” is entered, the code will generate all permutations of length 1 to 3 characters: a, b, c, ab, ac, ba, bc, ca, cb, abc, acb, bac, bca, cab, cba.

import java.util.ArrayList;


public class Permutation {
  private ArrayList permutations = null;
  private String characters = null;

  public Permutation(String characters) {
    if((characters == null) || ("").equals(characters.trim())) {
      characters = "";
    }
    this.characters = characters;
  }

  public String getCharacters() {
    return characters;
  }

  public ArrayList getPermutations() {
    this.permutations = new ArrayList();

    for(int j = 1; j <= getCharacters().length(); j++) {
      permute("",getCharacters(),j);
    }
    return this.permutations;
  }

  protected void permute(String prefix,String s,int length) {
    String word = null;

    for(int i = 0; i < s.length(); i++) {
      word = prefix+s.charAt(i);

      if(word.length() == length) {
        permutations.add(word);
      } else {
        if(i == 0) {
          permute(word,s.substring(i+1,s.length()),length);
        } else if(i == (s.length()-1)) {
          permute(word,s.substring(0,s.length()-1),length);
        } else {
          permute(word,(s.substring(0,i)+s.substring(i+1,s.length())),length);
        }
      }
    }
    return;
  }
}