Robot Class in Java
Robot Class in Java
The Robot class in the Java AWT package is used to generate native
system input events for the purposes of test automation, self-running
demos, and other applications where control of the mouse and keyboard is
needed. The primary purpose of Robot is to facilitate automated testing
of Java platform implementations. In simple terms, the class provides
control over the mouse and keyboard devices.
We can handle input,output,and basic operation of maouse events with the help of Java Robot class..
example of robot class...
// Java program to working of Robot class
//This program is for Windoes. It opens notepad and types a message.
import
java.awt.AWTException;
import
java.awt.Robot;
import
java.awt.event.KeyEvent;
import
java.io.*;
public
class
RobotInJava
{
public
static
void
main(String[] args)
throws
IOException,
AWTException, InterruptedException
{
String command =
"notepad.exe"
;
Runtime run = Runtime.getRuntime();
run.exec(command);
try
{
Thread.sleep(
2000
);
}
catch
(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create an instance of Robot class
Robot robot =
new
Robot();
// Press keys using robot. A gap of
// of 500 mili seconds is added after
// every key press
robot.keyPress(KeyEvent.VK_H);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_F);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_R);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_M);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_N);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_I);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_R);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_A);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_V);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_A);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_T);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(
600
);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(
600
);
}
}
Output:--
simple robot write "hello from niravpatel"
The code opens a blank Notepad file and types "hello from niravpatel" onto it with a delay of 500 ms before typing out each character.
Comments
Post a Comment