Sunday, September 20, 2009

Using Java Robot to type text strings

The Java Robot class is very useful for automating various tasks that normally require user interaction. However, it lacks a rather simple but useful feature - it cannot type a string of text, just a single key code. I had to implement this when I worked on one of my projects and to my surprise an extensive googling session showed that quite a lot of people are looking for such functionality.

The SmartRobot class below extends the standard Java Robot class adding to "type(String text)" and a few other useful methods:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class SmartRobot extends Robot {

public SmartRobot() throws AWTException {
super();
}

public void keyType(int keyCode) {
keyPress(keyCode);
delay(50);
keyRelease(keyCode);
}

public void keyType(int keyCode, int keyCodeModifier) {
keyPress(keyCodeModifier);
keyPress(keyCode);
delay(50);
keyRelease(keyCode);
keyRelease(keyCodeModifier);
}


public void type(String text) {
String textUpper = text.toUpperCase();

for (int i=0; i<text.length(); ++i) {
typeChar(textUpper.charAt(i));
}
}

private void typeChar(char c) {
boolean shift = true;
int keyCode;

switch (c) {
case '~':
keyCode = (int)'`';
break;
case '!':
keyCode = (int)'1';
break;
case '@':
keyCode = (int)'2';
break;
case '#':
keyCode = (int)'3';
break;
case '$':
keyCode = (int)'4';
break;
case '%':
keyCode = (int)'5';
break;
case '^':
keyCode = (int)'6';
break;
case '&':
keyCode = (int)'7';
break;
case '*':
keyCode = (int)'8';
break;
case '(':
keyCode = (int)'9';
break;
case ')':
keyCode = (int)'0';
break;
case ':':
keyCode = (int)';';
break;
case '_':
keyCode = (int)'-';
break;
case '+':
keyCode = (int)'=';
break;
case '|':
keyCode = (int)'\\';
break;
// case '"':

// keyCode = (int)'\'';

// break;

case '?':
keyCode = (int)'/';
break;
case '{':
keyCode = (int)'[';
break;
case '}':
keyCode = (int)']';
break;
case '<':
keyCode = (int)',';
break;
case '>':
keyCode = (int)'.';
break;
default:
keyCode = (int)c;
shift = false;
}
if (shift)
keyType(keyCode, KeyEvent.VK_SHIFT);
else
keyType(keyCode);
}

private int charToKeyCode(char c) {
switch (c) {
case ':':
return ';';
}
return (int)c;
}
}


The code can be downloaded here

Labels: , , , , , ,

2 Comments:

Blogger bim9262 said...

I've modified you code and fixed the code so that it can write upper and lowercase also fixed the double and single quote.

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

public class SmartRobot extends Robot {

public SmartRobot() throws AWTException {
super();
}

public void keyType(int keyCode) {
keyPress(keyCode);
delay(50);
keyRelease(keyCode);
}

public void keyType(int keyCode, int keyCodeModifier) {
keyPress(keyCodeModifier);
keyPress(keyCode);
delay(50);
keyRelease(keyCode);
keyRelease(keyCodeModifier);
}


public void type(String text) {

for (int i=0; i':
keyCode = (int)'.';
break;
default:
keyCode = (int)Character.toUpperCase(c);
shift = isUpper(c);
break;
}
if (shift)
keyType(keyCode, KeyEvent.VK_SHIFT);
else
keyType(keyCode);
}

private boolean isUpper(char c)
{
final char[] ALPHA="ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (char a:ALPHA) if(a==c) return true;
return false;
}

private int charToKeyCode(char c) {
switch (c) {
case ':':
return ';';
}
return (int)c;
}
}

2:03 PM  
Anonymous Anonymous said...

Hi. I did need all special chars,. Here's a modfied version which uses the clipboard instead of typing.

class SmartRobot extends Robot
{

public SmartRobot() throws AWTException
{
super();
}

/*public void pressEnter()
{
keyPress(KeyEvent.VK_ENTER);
delay(50);
keyRelease(KeyEvent.VK_ENTER);
} */

public void pasteClipboard()
{
keyPress(KeyEvent.VK_CONTROL);
keyPress(KeyEvent.VK_V);
delay(50);
keyRelease(KeyEvent.VK_V);
keyRelease(KeyEvent.VK_CONTROL);
}

public void type(String text)
{
writeToClipboard(text);
pasteClipboard();
}

private void writeToClipboard(String s)
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = new StringSelection(s);
clipboard.setContents(transferable, null);
}
}

4:04 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home