java.awt.Robot: a simple tutorial

Hello!  I have a very simple and friendly sample program here of using java.awt.Robot of Java API.
The code I have written pretty much explains the basic concepts you need to know about Robot class,  I have included Java Docs, and my comments in the source code.  Of course, for your learning pleasure.

Basically I have 3 classes here,  HappyHalloween class,  Keyboard class,  and the Mouse class.
Keyboard class and Mouse class are classes inside HappyHalloween class.  And you may use these 2 classes for your own app (like doing some overnight testings or whatever your imagination leads you)

Keyboard and Mouse classes are utilizing the Robot class (of java.awt package)

Here’s the source code:
////////////////////////////////////////////////////////////////////////

/*
* Happy Halloween!
*
* A spooky software for the Halloween!
*
* Scare your work mate, friends and classmate with this simple
* friendly java code.
* Also provides a simple demonstration about Java controlling
* your desktop natively.
*
* This may be compiled using javac HappyHalloween.java and run using java HappyHalloween
* OR
* Through Eclipse / Netbeans,
*
*
* (c)2008 Ryan G. Webb 2008/10/30 (just in time for Halloween!)
* The author of this code is not liable for any damage this may cause.
* Please include this last paragraph if you happen to use my Keyboard and Mouse Class.
*/

import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Robot;
import javax.swing.JOptionPane;

/**
*
* @author Ryan G. Webb 2008/10/30
*/
public class HappyHalloween {

/**
* Class Keyboard
* Emulates the typing process of a Physical Keyboard.
*
* @author Ryan G. Webb 2008/10/30
*/
private static class Keyboard {

private Robot robot;
private int typeSpeed;

/**
* Emulate the Pressing of a Physical keyboard.
* Letters are from 0x41 ~ 0x5A (Hex) equivalent to regular A ~ Z (ASCII)
* The Windows(TM) key on keyboard is 0x020c.

* For more information see JDK API on KeyEvent Topics AND/OR Refer to ASCII CHART
*
* @param letter array of primitive ints that represent keys of a keyboard.
* @author Ryan G. Webb 2008/10/30
*/
private void type(int[] letter) throws AWTException {
try {
robot = new Robot();
int len = letter.length;

//* If there’s a shift / alt key.
for (int i=0; i
robot.delay(this.getTypeSpeed());
if (letter[i] == 0x10 || letter[i] == 0x12) { //* alt = 0x12; shift = 0x10
robot.keyPress(letter[i]); //* Press Shift/Alt
robot.keyPress(letter[++i]); //* Press Key
robot.keyRelease(letter[i]); //* Release Key
robot.keyRelease(letter[–i]); //* Release Shift/Alt
i++;
} else {
robot.keyPress(letter[i]);
robot.keyRelease(letter[i]);
}
}
} finally {
robot = null;
}
}

/**
* @return typeSpeed a primitive int used to represent typing speed in ms.
*
* @author Ryan G. Webb 2008/10/30
*/
private int getTypeSpeed() {
return this.typeSpeed;
}

/**
* @param typeSpeed a primitive int used to represent typing speed in ms.
*
* @author Ryan G. Webb 2008/10/30
*/
private void setTypeSpeed(int typeSpeed) {
this.typeSpeed = typeSpeed;
}
}

/**
* Class Mouse
* Emulate the functions of a physical mouse
*
* @author Ryan G. Webb 2008/10/30
*/
private static class Mouse {

private Robot robot;
private int scrollSpeed;
private static int xStart = 613;
private static int yStart = 484;

/**
* Sets the mouse pointer at center screen, then moves to specified position
* using the X and Y Coordinates.
*
* @param x a primitive int that represents position in relevance to vertical
* @param y a primitive int that represents position in relevance to horizontal
* @param click a primitive boolean that tells if a click action is necessary (true=click action)
*
* @author Ryan G. Webb 2008/10/30
*/
private void move(int x, int y, boolean click) throws AWTException {
try {
robot = new Robot();
//* Initialize starting position estimated: center of screen
robot.mouseMove(xStart, yStart);
while (xStart != x || yStart != y) {

int xAxis, yAxis;
if (xStart < x) {
xAxis = ++xStart;
} else if (xStart > x){
xAxis = –xStart;
} else {
xAxis = xStart;
}

if (yStart < y) {
yAxis = ++yStart;
} else if(yStart > y){
yAxis = –yStart;
} else {
yAxis = yStart;
}

robot.mouseMove(xAxis, yAxis);
robot.delay(this.getScrollSpeed());
}

if (click)
leftClick();
} finally {
robot = null;
}
}

/**
* Provides a simulation of a mouse left click.
*
* @author Ryan G. Webb 2008/10/30
*/
private void leftClick() throws AWTException, IllegalArgumentException {
try {
robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} finally {
robot = null;
}
}

/**
* @return scrollSpeed a primitive int used to represent scroll speed in ms.
*
* @author Ryan G. Webb 2008/10/30
*/
private int getScrollSpeed() {
return this.scrollSpeed;
}

/**
* @param scrollSpeed a primitive int used to represent scroll speed in ms.
*
* @author Ryan G. Webb 2008/10/30
*/
private void setScrollSpeed(int scrollSpeed) {
this.scrollSpeed = scrollSpeed;
}
}

/*
* Main Program for running Happy Halloween class
*/
public static void main(String[] args) {
int[] executeNotepad = { 0x020C, 0x52, 0x4E, 0x4F, 0x54, 0x45, 0x50, 0x41, 0x44, KeyEvent.VK_ENTER };
int[] maximize = { 0x12, 0x20, 0x58 };
int[] message = { 0x10, 0x48, 0x41, 0x50, 0x50, 0x59, 0x20, 0x10, 0x48, 0x41, 0x4C, 0x4C, 0x4F, 0x57, 0x45, 0x45, 0x4E, 0x10, 0x31 };
Keyboard k = new Keyboard();
Mouse m = new Mouse();

try {
k.setTypeSpeed(500);
k.type(executeNotepad);
k.type(maximize);
k.type(message);

m.setScrollSpeed(2);
m.move(11, 35, true); //* moves to File then click
m.move(40, 104, true); //* moves to Save As then click
} catch (AWTException e) {
//* Display a message box telling the user what happened
JOptionPane.showMessageDialog(null, e.getMessage(), “Happy Halloween!”, JOptionPane.ERROR_MESSAGE);
} catch (IllegalArgumentException e) {
//* Display a message box telling the user what happened
JOptionPane.showMessageDialog(null, e.getMessage(), “Happy Halloween!”, JOptionPane.ERROR_MESSAGE);
}
}
}

////////////////////////////////////////////////////////////////////////

Don’t worry if you see some Hex code like 0x10 it means Shift (I believe I put that as comment).  I used Hex because I think it looks cool!
0x41 – A
0x42 – B
.
.
.
And so on…(Refer to java.awt.event.KeyEvent for code constants)

you may also put a char literal instead of a hex code like this:

int[] message = { 0x10, ‘H’, ‘A’, ‘P’, ‘P’, ‘Y’, 0x20, 0x10, ‘H’, ‘A’, ‘L’, ‘L’, ‘O’, ‘W’, ‘E’, ‘E’, ‘N’, 0x10, ‘1’ };

make sure you type UPPERCASE char literal lowercase literals doesn’t work!  So put a 0x10 (Shift key) if you want to produce uppercase.

I hope you learned / gained something…

Many thanks!

(comments / sugestions / errata about this subject is very much welcome)