In continuation to the blog on "Screen capture in java", this post shows an example of how java.awt.Robot can be used for automation and create your own handy custom made automations. Running this example will open up firefox in your system and types in the twitter.com and loads the page for you.
Try it yourself, it was nice to see it working.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class RobotSample {
public static void main(String[] args) throws IOException {
try {
Robot robot = new Robot();
Runtime runtime = Runtime.getRuntime();
runtime.exec("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
robot.delay(1000);
robot.keyPress(KeyEvent.VK_F6);
robot.keyPress(KeyEvent.VK_DELETE);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_T);
robot.delay(70);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_DECIMAL);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(200);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Checkout In Dzone
No comments:
Post a Comment