Robot class of java is mainly used for self running demos and automated testing for java. This class is capable of generating native system input events. It can generate events like mouse move, mouse pressed, key pressed, key released etc. If you use robot.mouseMove then it will actually move mouse pointer instead-of generating only event. This class will also help you to take screen shot using program.
Class shows two constructors,
1. Robot(): It will create Robot object for primary screen.
2. Robot(GraphicsDevice screen): It will generate Robot object for given screen device.
This class interacts with low level system components. If permissions are restricted on your system the it will throw AWTException.
Here is sample program for this class,
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args) {
try {
//Create object of Robot class
Robot robot = new Robot();
/*It will generate key press event.
In the same way you can use different key options
insted of VK_B. In this case b will be printed at your cursor.*/
robot.keyPress(KeyEvent.VK_B);
/* Releases given key.*/
robot.keyRelease(KeyEvent.VK_B);
/*Mouse move will move mouse cursor to given position.*/
robot.mouseMove(100, 500);
/*Keep your mouse pointer moving.*/
int j=10;
for(int i=0; i<50; i++) {
robot.delay(500);
j+=10;
robot.mouseMove(j, 10);
}
/*Mouse press will generate mouse click event at mouse pointer.*/
robot.mousePress(0);
System.out.println("Before delay.");
//Delay. Here delay is given in milliseconds.
robot.delay(5000);
System.out.print("After delay.");
} catch (AWTException e) {
System.out.print("\nError : " + e);
}
}
}
Here I have explained some simple features and basic use of Robot class. It is with other features as well.
Class shows two constructors,
1. Robot(): It will create Robot object for primary screen.
2. Robot(GraphicsDevice screen): It will generate Robot object for given screen device.
This class interacts with low level system components. If permissions are restricted on your system the it will throw AWTException.
Here is sample program for this class,
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args) {
try {
//Create object of Robot class
Robot robot = new Robot();
/*It will generate key press event.
In the same way you can use different key options
insted of VK_B. In this case b will be printed at your cursor.*/
robot.keyPress(KeyEvent.VK_B);
/* Releases given key.*/
robot.keyRelease(KeyEvent.VK_B);
/*Mouse move will move mouse cursor to given position.*/
robot.mouseMove(100, 500);
/*Keep your mouse pointer moving.*/
int j=10;
for(int i=0; i<50; i++) {
robot.delay(500);
j+=10;
robot.mouseMove(j, 10);
}
/*Mouse press will generate mouse click event at mouse pointer.*/
robot.mousePress(0);
System.out.println("Before delay.");
//Delay. Here delay is given in milliseconds.
robot.delay(5000);
System.out.print("After delay.");
} catch (AWTException e) {
System.out.print("\nError : " + e);
}
}
}
Here I have explained some simple features and basic use of Robot class. It is with other features as well.
No comments:
Post a Comment