Java Keypress Generator

The following is a keypress generator using the Robot class in Java. If you provide a string to the constructor and call the simulate() method, each of the characters in the string is generated as a keypress event. In other words, the program simulates the pressing of a key on physical keyboard. This may be used to redirect input to another window.

Usage

java Typer [options] [string] [string] [...]

Options
--delay Sets the processing interval between strings in milliseconds [default: 0 (0 milliseconds)]

[string] represents the set of characters used to generate keypress events; multiple strings may be passed when separated by a space.

Example

The following illustrates the code being compiled and executed using the sample input “test1”, “test2”, “test3” and “test4”. Each keypress event is delayed by 2 seconds (2000 milliseconds). The program is executed and I immediately (manually) move the window focus to the Notepad window. As the program executes, the generated keypress events are received in Notepad because that is the window with active focus.

Executing the Java Keypress Generator
Executing the Keypress Generator

Source Code

import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Typer {
  private Robot robot = null;
  private String characters = null;
  private int iDelay = 0;
  public static void main(String[] args) {
    Typer typerBot = null;
    int iDelay = 0;
    boolean bDelayArg = false;
    for(String s: args) {
      if(bDelayArg) {
        bDelayArg = false;
        try {
          iDelay = Integer.parseInt(s);
          if(iDelay < 0) {
            throw new NumberFormatException("Negative Integer");
          }
        } catch (NumberFormatException e) {
          System.out.println("Error: Delay must be a positive integer in milliseconds.");
          System.exit(1);
        }
      }
      if(s.equalsIgnoreCase("--delay")) {
        bDelayArg = true;
      }
    }
    bDelayArg = false;
    for(String s: args) {
      if(s.equalsIgnoreCase("--delay")) {
        bDelayArg = true;
      } else {
        if(!bDelayArg) {
          System.out.println("Initiating simulation for string: "+s);
          typerBot = new Typer(s, iDelay);
          typerBot.delay();
          typerBot.simulate();
          System.out.println("");
        } else {
          bDelayArg = false;
        }
      }
    }
 
    return;
  }
  public Typer(String characters) {
    if((characters == null) || ("").equals(characters.trim())) {
      characters = "";
    }
    this.characters = characters;
    try {
      robot = new Robot();
    } catch(Exception e) {
    }
    return;
  }
  public Typer(String characters, int iDelay) {
    if((characters == null) || ("").equals(characters.trim())) {
      characters = "";
    }
    this.characters = characters;
    this.iDelay = iDelay;
    try {
      robot = new Robot();
    } catch(Exception e) {
    }
    return;
  }
  public String getCharacters() {
    return characters;
  }
  protected boolean delay() {
    if(this.robot != null) {
      robot.delay(this.iDelay);
      return true;
    }
    return false;
  }
  protected boolean delay(int iDelay) {
    if(this.robot != null) {
      robot.delay(iDelay);
      return true;
    }
    return false;
  }
  private int getKeyCode(char c) {
    int iKeyCode = 0;
    try {
      iKeyCode = KeyEvent.getExtendedKeyCodeForChar(c);
    } catch(Exception e) {
    }
  
    return iKeyCode;
  }
  public boolean simulate() {
    int iKeyCode = 0;
    if(robot == null) {
      return false;
    }
    for(int i = 0; i < this.getCharacters().length(); i++) {
      iKeyCode = getKeyCode(this.getCharacters().charAt(i));
      robot.keyPress(iKeyCode);
      robot.keyRelease(iKeyCode);
    }
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    return true;
  }
}

Leave a Comment