Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use Java's Robot class to type. This is what I have done so far.

robot = JavaNew["java.awt.Robot"]

The mouseMove command for the robot works fine, but when I try


I am given this error:

Java::argx1: Method named keyPress defined in class java.awt.Robot was called with an incorrect number or type of arguments. The argument was KeyEvent.VK_Y.

Calling the function for a robot object using this parameter works fine in a Java IDE, but I can not get it to work in Mathematica. What am I doing wrong?

share|improve this question
keyPress expects an integer and not a string. since keyPress expects an int there is even no method invocation conversion...if your IDE allows you that, then something is going completely wrong... – Stefan Jun 4 '13 at 18:11

JLink KeyEvents

In order to use the static fields from java.awt.event.KeyEvent you need to replace your *VK_Y* with VKUY. This is needed, since _ has a special meaning in Mathematica.

So the letter 'U' is just for 'Underscore'.

Your code will work correctly if you write it that way:

robot = JavaNew["java.awt.Robot"]


I was thinking about on how to use java.awt.Robot and came up with this idea. I'm using here the JavaReloader package from Leonid. If you have any problems to set it up on Linux/MacOSX just notify me and I can help you out...

Here is a Keyboard class:

javaCode = "
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.AWTException;

public class Keyboard {

  private Robot robot;

  public Keyboard() throws AWTException {
      this.robot = new Robot();

  public Keyboard(Robot robot) {
      this.robot = robot;

  public void type(CharSequence characters) {
      int length = characters.length();
      for (int i = 0; i < length; i++) {
          char character = characters.charAt(i);

  public void type(char character) {
      switch (character) {
      case 'a': doType(KeyEvent.VK_A); break;
      case 'b': doType(KeyEvent.VK_B); break;
      case 'c': doType(KeyEvent.VK_C); break;
      case 'd': doType(KeyEvent.VK_D); break;
      case 'e': doType(KeyEvent.VK_E); break;
      case 'f': doType(KeyEvent.VK_F); break;
      case 'g': doType(KeyEvent.VK_G); break;
      case 'h': doType(KeyEvent.VK_H); break;
      case 'i': doType(KeyEvent.VK_I); break;
      case 'j': doType(KeyEvent.VK_J); break;
      case 'k': doType(KeyEvent.VK_K); break;
      case 'l': doType(KeyEvent.VK_L); break;
      case 'm': doType(KeyEvent.VK_M); break;
      case 'n': doType(KeyEvent.VK_N); break;
      case 'o': doType(KeyEvent.VK_O); break;
      case 'p': doType(KeyEvent.VK_P); break;
      case 'q': doType(KeyEvent.VK_Q); break;
      case 'r': doType(KeyEvent.VK_R); break;
      case 's': doType(KeyEvent.VK_S); break;
      case 't': doType(KeyEvent.VK_T); break;
      case 'u': doType(KeyEvent.VK_U); break;
      case 'v': doType(KeyEvent.VK_V); break;
      case 'w': doType(KeyEvent.VK_W); break;
      case 'x': doType(KeyEvent.VK_X); break;
      case 'y': doType(KeyEvent.VK_Y); break;
      case 'z': doType(KeyEvent.VK_Z); break;
      case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break;
      case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break;
      case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break;
      case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break;
      case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break;
      case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break;
      case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break;
      case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break;
      case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break;
      case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break;
      case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break;
      case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break;
      case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break;
      case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break;
      case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break;
      case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break;
      case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break;
      case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break;
      case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break;
      case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break;
      case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break;
      case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break;
      case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break;
      case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break;
      case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break;
      case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break;
      case '!': doType(KeyEvent.VK_EXCLAMATION_MARK); break;
      case ' ': doType(KeyEvent.VK_SPACE); break;   
          throw new IllegalArgumentException(\"Cannot type character \" + character);

  private void doType(int... keyCodes) {
      doType(keyCodes, 0, keyCodes.length);

  private void doType(int[] keyCodes, int offset, int length) {
      if (length == 0) {

      doType(keyCodes, offset + 1, length - 1);


Using the JavaReloader is easy:

<< JavaReloader`


keyboard = JavaNew["Keyboard"]

And now you can do something like:

keyboard@type[JavaNew["java.lang.String", "Just Another Mathematica Hacker"]]

And it will write the CharSequence right into your notebook :)

--> Just Another Mathematica Hacker

share|improve this answer

After posting this question I was fiddling more with the whole "KeyEvent.VK" mess and saw that the parameter for keyPress and keyRelease is an int. I still do not know why the "KeyEvent.VK_" will not work in Mathematica, but I found this website with an integer code for each key on a standard keyboard and it works when applied to the keyPress function:

So using this,


actually types the "Y" character, while


still gave an error.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.