Today we will be looking at the Robot class. I find this to be quite fun and is very useful, especially in the making of bots in java 
The Robot class makes it possible for the java program to take over the control of the mouse and keyboard input at the operating system level.
(lol so you dont wanna leave it in an infinite loop or it might be a pain to close :P )
So lets get started with an example.
First of all you need the import
java.awt.Robot
We should probably declase a method 
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class testRobot{
public static void main(String[] args) throws AWTException{
Robot myRobot = new Robot();
}
}
The reason we have throws AWTException is because it is needed when using the robot class.
The application will throw that exception if the platform configuration does not allow low-level input control.
Another exception that can be thrown however is SecurityException but I wont go into that.
Ok I did say earlier that you are able to control the mouse and keyboard, so here were go 
but lets learn the functions first.
mouseMove(int x,int y) - Moves the mouse pointer to a set of specified absolute screen coordinates given in pixels.
mousePress(int button) - Presses one of the buttons on the mouse.
mouseRelease(int button) - Releases one of the buttons on the mouse.
keyPress(int key) - Presses a specified key on the keyboard.
keyRelease(int key) - Releases specified key on the keyboard.
all the numbers for the buttons and keys are stored in a class so dont worry about that to much. Hey I got a good Idea how about we test it by making it type in notepad 
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class testRobot{
public static void main(String[] args) throws AWTException{
Static int keys = {
KeyEvent.VK_D,
KeyEvent.VK_E,
KeyEvent.VK_V,
KeyEvent.VK_E,
KeyEvent.VK_R,
KeyEvent.VK_E,
KeyEvent.VK_U,
KeyEvent.VK_X
} //Spells devereux :D
Runtime.getRuntime().exec("notepad");
}
}
:O we have just opened notepad, and the keys that we want to type are stored in a variable, thats some pretty incredible shit right there :P
[code]
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class testRobot{
public static void main(String[] args) throws AWTException{
Static int keys = {
KeyEvent.VK_D,
KeyEvent.VK_E,
KeyEvent.VK_V,
KeyEvent.VK_E,
KeyEvent.VK_R,
KeyEvent.VK_E,
KeyEvent.VK_U,
KeyEvent.VK_X
} //Spells devereux :D
Runtime.getRuntime().exec("notepad");
Robot myRobot = new Robot();
myRobot.keyPress(KeyEvent.VK_SHIFT);
for(int loop = 0; loop < keys.length; loop++){
if(loop > 0){
myRobot.keyRelease(KeyEvent.VK_SHIFT);
}
myRobot.KeyPress(keys[loop]);
myRobot.delay(500);
myRobot.KeyRelease(keys[loop]);
}
}
}
Guess what that will do, it will first hold down shift, but after the first character is typed it releases shift and continues the typing. There is also about a half second delay in pressing and releasing of the keys just so that it has time to press it properly.
Credits to: http://java.sun.com/j2se/1.4.2/docs/...awt/Robot.html
Similar Threads: