Magazine
 
Tips & Tricks
 

1.Opening, writing, saving notepad automatically with Robot class in Java

Java provides a lot of fun while programming. This article shows you how to use Java.awt.Robot class that is both useful and fun. Robot class is used to take the control of mouse and keyboard, which lets you do any mouse and keyboard related operation through java code for the purposes of test automation, self-running demos. The sample code given below demonstrates handling of the keyboard events. When this program is run, it does a series of events automatically. It opens notepad and types “javajazzup” in it and saves this file as “robo.txt” automatically.

RobotExample.java

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotExample {
//Store Keystrokes in an array for the content in notepad
static int keyInputForNotepadContent[] = {KeyEvent.VK_J,KeyEvent.VK_A, KeyEvent.VK_V,KeyEvent.VK_A,KeyEvent.VK_J ,KeyEvent.VK_A,KeyEvent.VK_Z, KeyEvent.VK_Z,KeyEvent.VK_U,KeyEvent.VK_P};
//Store Keystrokes in an array to give name of the notepad
static int keyInputForNotepadName[] = {KeyEvent.VK_R,KeyEvent.VK_O, KeyEvent.VK_B,KeyEvent.VK_O, KeyEvent.VK_DECIMAL,KeyEvent.VK_T, KeyEvent.VK_X,KeyEvent.VK_T};
public static void main(String[] args) {
try { Robot robot = new Robot();
//Press keys Ctrl+Esc and then key R to open “run” program.
robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_ESCAPE); robot.keyRelease(KeyEvent.VK_CONTROL);

  robot.keyRelease(KeyEvent.VK_ESCAPE); robot.keyPress(KeyEvent.VK_R);
//Type NOTEPAD in run program and press ENTER ket to open a notepad.
robot.keyPress(KeyEvent.VK_N); robot.keyPress(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_T); robot.keyPress(KeyEvent.VK_E); robot.keyPress(KeyEvent.VK_P); robot.keyPress(KeyEvent.VK_A); robot.keyPress(KeyEvent.VK_D); robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(1000);

// Creates the delay of 1 sec
//Press keys of characters to write on the notepad.

for (int i = 0; i < keyInputForNotepadContent.length; i++){ robot.keyPress(keyInputForNotepadContent[i]); robot.delay(500);
}
robot.delay(1000);

// Press keys ALT+F+S to save the file

robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_F); robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_ALT);
robot.delay(500);
//Press keys of characters to write the name of notepad.
for (int i = 0; i < keyInputForNotepadName.length; i++){ robot.keyPress(keyInputForNotepadName[i]); robot.delay(500);
}
robot.keyPress(KeyEvent.VK_ENTER);
// Replace the file if it already exists. robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_ALT);
}
catch (AWTException e) {
e.printStackTrace();
}
}
}
Dec 2007 | Java Jazz Up | 70
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

Download PDF