Monday, October 19, 2015

JAVA ROBOT CODE TO DO SOME PREDEFINE ACTIVITY

THIS CODE WILL TAKE UPDATE IN YOUR PARTICULAR FOLDER FOR THE CONFIGURED WORKSPACE.

IT WILL OPEN YOUR PREDEFINE SOFTWARE (i.e. Eclipse, ThunderBird etc)

IT WILL OPEN YOUR SOME ROUTINE SITES (i.e Gmail, Hotmail etc etc)

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/*Author : RAJ*/

class MyRobot {

Robot robot = new Robot();

public static void main(String[] args) {

try {

Desktop desktop = Desktop.getDesktop();
File dirToOpen = new File("D:\\Data\\My_Branch");
desktop.open(dirToOpen);

MyRobot r = new MyRobot();

// Creates the delay of 5 sec so that you can open notepad before
// Robot start writting
/*
* robot.delay(5000); robot.keyPress(KeyEvent.VK_H);
* robot.keyPress(KeyEvent.VK_I); robot.keyPress(KeyEvent.VK_SPACE);
* robot.keyPress(KeyEvent.VK_B); robot.keyPress(KeyEvent.VK_U);
* robot.keyPress(KeyEvent.VK_D); robot.keyPress(KeyEvent.VK_Y);
*/

} catch (Exception e) {
e.printStackTrace();
}
}

public MyRobot() throws AWTException {
startTheBattle();
}

private void leftClick() {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}

private void startTheBattle(){
rightClick();
leftClick();
robot.delay(1000);

openChrome();
robot.delay(1000);

openEclipse();
robot.delay(1000);

openThunder();
exitNow();
}

private void exitNow() {
System.out.println("Exiting Now.. Good Bye..");
System.exit(0);
}

private void rightClick() {
robot.mouseMove(640, 630);
robot.delay(500);

robot.mousePress(InputEvent.BUTTON3_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
robot.delay(200);

robot.mouseMove(660, 500);
robot.delay(500);
}

private void openChrome(){
try{
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://gmail.com"));
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://yahoomail.com"));
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://hotmail.com"));
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://google.com"));
}catch(Exception ex){}
}


private void openEclipse(){
try{
String path = "D:\\Data\\Software\\eclipse-jee-indigo-SR2-win32-x86_64 - Branch-660\\eclipse\\eclipse.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
}catch(Exception ex){}
}

private void openThunder(){
try{
String path = "D:\\Data\\Software\\ThunderbirdPortable\\ThunderbirdPortable.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
}catch(Exception ex){}
}

private void type(int i) {
robot.delay(40);
robot.keyPress(i);
robot.keyRelease(i);
}

private void type(String s) {
byte[] bytes = s.getBytes();
for (byte b : bytes) {
int code = b;
// keycode only handles [A-Z] (which is ASCII decimal [65-90])
if (code > 96 && code < 123)
code = code - 32;
robot.delay(40);
robot.keyPress(code);
robot.keyRelease(code);
}
}
}

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...