April 14, 2015

Automatically typing notepad in java


A Notepad that works automatically in java


/**
 * Funny notepad automatically wishing birthday
 */
package com.blogspot.javabelazy.funny;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @author +Vipin Cp
 *
 */
public class HappyBirthdayMessage {

/**
* @param tokyo
*/
public static void main(String[] tokyo) {
// TODO Auto-generated method stub
HappyBirthdayMessage message = new HappyBirthdayMessage();
String yourMessage = "Happy Birthday Jerin V George";
Runtime runtime = Runtime.getRuntime();
try {
                       Thread.sleep(5000);
runtime.exec("notepad");
Thread.sleep(3000);
message.createMessage(yourMessage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void createMessage(String message) {
// TODO Auto-generated method stub
try {
Robot robot = new Robot();
int length = message.length();
for(int i=0;i<length;i++){
char letter = message.charAt(i);
String keyVal = Character.toString(letter);
String variableName;
if(keyVal.equals(" ")){
variableName ="VK_SPACE";
}else{
variableName = "VK_"+keyVal.toUpperCase();
}


Class clazz = KeyEvent.class;
   Field field = clazz.getField(variableName);
   int keyCode = field.getInt(null);
   robot.keyPress(KeyEvent.VK_SHIFT);
   robot.keyPress(keyCode);
   robot.delay(1000);
}

} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Java tips and trick by +belazy

http://javabelazy.blogspot.in/

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments