HOME

IV - Creating output


Controlling the mouse cursor


This is the easiest part of the bot programming. You simply use the java.awt.Robot library to point and click wherever you want on the screen.

The origo (x=0, y=0) of the coordinate system used by the Robot is located at the top left of the screen.
Below is an example of how the Robot library can be implemented. X and Y are given in pixels.


int X = x-coord;
int Y = y-coord;

try {
Robot rob = new Robot();
rob.mouseMove(X, Y);
rob.delay(100);
rob.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
rob.delay(100);
rob.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
}

catch (AWTException e) {
showMessageDialog(null, "Robot could not be created!");
System.exit(0);
}


Note that you will have to have a short delay between pressing and releasing the mouse button to allow the program that the bot is interacting with some time to register the click.




Using the keyboard

This can also be done using the java.awt.Robot library, using the keyPress and keyRelease methods. If you have to type in something in a box for example you just move the mouse and activate the box first as described above.
The input to the methods are in the form of key codes. For example the letter A in key code is KeyEvent.VK_A .


Rev. 2 (11 Jan. -09)





<<<Chapter IIIChapter V>>>