This post will help you emulate your keyboard keys using Java. This program opens up notepad and start typing on notepad. The best part is we introduced delay after typing of each letter which makes it look like a bot is typing.
Features of program:
1) Program first runs notepad
3) Program will type a message letter by letter with small delay on notepad
4) For example it display a message "Hi, Did you watch the Terminator. The second part was awesome."
Reference:
http://stackoverflow.com/questions/1248510/convert-string-to-keyevents
Language Used:
Java
Git Location:
https://github.com/csanuragjain/extra/tree/master/EmulateKeyboard
Program:
Constructor:
Features of program:
1) Program first runs notepad
3) Program will type a message letter by letter with small delay on notepad
4) For example it display a message "Hi, Did you watch the Terminator. The second part was awesome."
Reference:
http://stackoverflow.com/questions/1248510/convert-string-to-keyevents
Language Used:
Java
Git Location:
https://github.com/csanuragjain/extra/tree/master/EmulateKeyboard
Program:
Constructor:
private Robot robot;
public EmulateKeyboard() throws AWTException {
this.robot = new Robot();
}
public EmulateKeyboard(Robot robot) {
this.robot = robot;
}
How it works:
1) EmulateKeyboard is the name of class
2) Robot is a predefined class which actually provides the functionality of typing keyboard events
3) We define 2 constructor which instantiate the Robot class object.
main method:
How it works:
1) We open notepad using Runtime.getRuntime.exec function
2) We make an object of our class and call the type function
3) type function take 2 argument
4) The first argument denote the message to be written
5) Second argument tells the delay belay typing of each letter of message to be printed.
6) So, program will open notepad and will start writing each letter of message with a delay of 50ms
type method:
How it works:
1) We convert the string into character array
2) For each character we call typeChar which actually types the character.
3) After the character is written we call the delay method of Robot class which introduces a delay of milliseconds passed in 2nd argument
4) After delay is completed next characters goes again through step2 & step3
typeChar method:
How it works:
1) This method receives the character to be written
2) It passes the character to a simple switch
3) So for instance if character was 'a' then first case will call doType method with argument KeyEvent.VK_A
4) KeyEvent.VK_A is the keycode for letter 'a'
5) For cases like 'A' the trick is to pass 2 keys to be pressed which are KeyEvent.VK_SHIFT & KeyEvent.VK_A. This makes program to press shift+a which becomes A
doType method:
How it works:
1) This function obtains the keycode from typeChar function. Now this could be single value('a') or multiple keycodes ('A')
2) This makes a call to dotypechar along with the keycodes and its start and offset value
3) Changing this function as protected so that it can be called by other classes.
dotypeChar method:
How it works:
1) This method finally writes the keycodes
2) First we check if the keycode length is not zero. If zero then nothing to do
3) keypress method comes with Robot class which emulates a key press. It takes the keycode to be typed
4) Here we call the keypress method and pass the keycode[0] we got in argument
5) We make a recursive call to doTypeChar so that if we have 2 keycodes like 'shift+a' then shift had been taken care at step4 but for printing a we again need to make recursive call. In case this function was called to print only single keycode like for only letter 'a' then recursive call to dotypechar will do nothing since length would be zero as checked in step2
6) Now we call keyrelease method of Robot class which releases the key pressed at previous step.
You can also override the typeChar method to add more characters along with the old one's using below method in your subclass:
Output:
Full Program:
Hope it helps :)
1) EmulateKeyboard is the name of class
2) Robot is a predefined class which actually provides the functionality of typing keyboard events
3) We define 2 constructor which instantiate the Robot class object.
main method:
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("notepad.exe");
EmulateKeyboard keyboard = new EmulateKeyboard();
keyboard.type("Hi, Did you watch the Terminator?\nThe second part was awesome.",50);
}
How it works:
1) We open notepad using Runtime.getRuntime.exec function
2) We make an object of our class and call the type function
3) type function take 2 argument
4) The first argument denote the message to be written
5) Second argument tells the delay belay typing of each letter of message to be printed.
6) So, program will open notepad and will start writing each letter of message with a delay of 50ms
type method:
public void type(CharSequence characters,int delay) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
typeChar(character);
this.robot.delay(delay);
}
}
How it works:
1) We convert the string into character array
2) For each character we call typeChar which actually types the character.
3) After the character is written we call the delay method of Robot class which introduces a delay of milliseconds passed in 2nd argument
4) After delay is completed next characters goes again through step2 & step3
typeChar method:
public void typeChar(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case 'e': doType(KeyEvent.VK_E); break;
case 'f': doType(KeyEvent.VK_F); break;
case 'g': doType(KeyEvent.VK_G); break;
case 'h': doType(KeyEvent.VK_H); break;
case 'i': doType(KeyEvent.VK_I); break;
case 'j': doType(KeyEvent.VK_J); break;
case 'k': doType(KeyEvent.VK_K); break;
case 'l': doType(KeyEvent.VK_L); break;
case 'm': doType(KeyEvent.VK_M); break;
case 'n': doType(KeyEvent.VK_N); break;
case 'o': doType(KeyEvent.VK_O); break;
case 'p': doType(KeyEvent.VK_P); break;
case 'q': doType(KeyEvent.VK_Q); break;
case 'r': doType(KeyEvent.VK_R); break;
case 's': doType(KeyEvent.VK_S); break;
case 't': doType(KeyEvent.VK_T); break;
case 'u': doType(KeyEvent.VK_U); break;
case 'v': doType(KeyEvent.VK_V); break;
case 'w': doType(KeyEvent.VK_W); break;
case 'x': doType(KeyEvent.VK_X); break;
case 'y': doType(KeyEvent.VK_Y); break;
case 'z': doType(KeyEvent.VK_Z); break;
case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break;
case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break;
case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break;
case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break;
case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break;
case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break;
case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break;
case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break;
case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break;
case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break;
case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break;
case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break;
case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break;
case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break;
case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break;
case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break;
case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break;
case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break;
case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break;
case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break;
case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break;
case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break;
case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break;
case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break;
case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break;
case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break;
case '`': doType(KeyEvent.VK_BACK_QUOTE); break;
case '0': doType(KeyEvent.VK_0); break;
case '1': doType(KeyEvent.VK_1); break;
case '2': doType(KeyEvent.VK_2); break;
case '3': doType(KeyEvent.VK_3); break;
case '4': doType(KeyEvent.VK_4); break;
case '5': doType(KeyEvent.VK_5); break;
case '6': doType(KeyEvent.VK_6); break;
case '7': doType(KeyEvent.VK_7); break;
case '8': doType(KeyEvent.VK_8); break;
case '9': doType(KeyEvent.VK_9); break;
case '-': doType(KeyEvent.VK_MINUS); break;
case '=': doType(KeyEvent.VK_EQUALS); break;
case '~': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE); break;
case '!': doType(KeyEvent.VK_EXCLAMATION_MARK); break;
case '@': doType(KeyEvent.VK_AT); break;
case '#': doType(KeyEvent.VK_NUMBER_SIGN); break;
case '$': doType(KeyEvent.VK_DOLLAR); break;
case '%': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5); break;
case '^': doType(KeyEvent.VK_CIRCUMFLEX); break;
case '&': doType(KeyEvent.VK_AMPERSAND); break;
case '*': doType(KeyEvent.VK_ASTERISK); break;
case '(': doType(KeyEvent.VK_LEFT_PARENTHESIS); break;
case ')': doType(KeyEvent.VK_RIGHT_PARENTHESIS); break;
case '_': doType(KeyEvent.VK_UNDERSCORE); break;
case '+': doType(KeyEvent.VK_PLUS); break;
case '\t': doType(KeyEvent.VK_TAB); break;
case '\n': doType(KeyEvent.VK_ENTER); break;
case '[': doType(KeyEvent.VK_OPEN_BRACKET); break;
case ']': doType(KeyEvent.VK_CLOSE_BRACKET); break;
case '\\': doType(KeyEvent.VK_BACK_SLASH); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;
case ';': doType(KeyEvent.VK_SEMICOLON); break;
case ':': doType(KeyEvent.VK_COLON); break;
case '\'': doType(KeyEvent.VK_QUOTE); break;
case '"': doType(KeyEvent.VK_QUOTEDBL); break;
case ',': doType(KeyEvent.VK_COMMA); break;
case '<': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_COMMA); break;
case '.': doType(KeyEvent.VK_PERIOD); break;
case '>': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_PERIOD); break;
case '/': doType(KeyEvent.VK_SLASH); break;
case '?': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH); break;
case ' ': doType(KeyEvent.VK_SPACE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
How it works:
1) This method receives the character to be written
2) It passes the character to a simple switch
3) So for instance if character was 'a' then first case will call doType method with argument KeyEvent.VK_A
4) KeyEvent.VK_A is the keycode for letter 'a'
5) For cases like 'A' the trick is to pass 2 keys to be pressed which are KeyEvent.VK_SHIFT & KeyEvent.VK_A. This makes program to press shift+a which becomes A
doType method:
protected void doType(int... keyCodes) {
doTypeChar(keyCodes, 0, keyCodes.length);
}
How it works:
1) This function obtains the keycode from typeChar function. Now this could be single value('a') or multiple keycodes ('A')
2) This makes a call to dotypechar along with the keycodes and its start and offset value
3) Changing this function as protected so that it can be called by other classes.
dotypeChar method:
private void doTypeChar(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doTypeChar(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
How it works:
1) This method finally writes the keycodes
2) First we check if the keycode length is not zero. If zero then nothing to do
3) keypress method comes with Robot class which emulates a key press. It takes the keycode to be typed
4) Here we call the keypress method and pass the keycode[0] we got in argument
5) We make a recursive call to doTypeChar so that if we have 2 keycodes like 'shift+a' then shift had been taken care at step4 but for printing a we again need to make recursive call. In case this function was called to print only single keycode like for only letter 'a' then recursive call to dotypechar will do nothing since length would be zero as checked in step2
6) Now we call keyrelease method of Robot class which releases the key pressed at previous step.
You can also override the typeChar method to add more characters along with the old one's using below method in your subclass:
@Override
public void typeChar(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); return;
}
super.typeChar(character);
}
Output:
Full Program:
package com.cooltrickshome;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class EmulateKeyboard {
private Robot robot;
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("notepad.exe");
EmulateKeyboard keyboard = new EmulateKeyboard();
keyboard.type("Hi, Did you watch the Terminator?\nThe second part was awesome.",50);
}
public EmulateKeyboard() throws AWTException {
this.robot = new Robot();
}
public EmulateKeyboard(Robot robot) {
this.robot = robot;
}
public void type(CharSequence characters,int delay) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
typeChar(character);
this.robot.delay(delay);
}
}
public void typeChar(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case 'e': doType(KeyEvent.VK_E); break;
case 'f': doType(KeyEvent.VK_F); break;
case 'g': doType(KeyEvent.VK_G); break;
case 'h': doType(KeyEvent.VK_H); break;
case 'i': doType(KeyEvent.VK_I); break;
case 'j': doType(KeyEvent.VK_J); break;
case 'k': doType(KeyEvent.VK_K); break;
case 'l': doType(KeyEvent.VK_L); break;
case 'm': doType(KeyEvent.VK_M); break;
case 'n': doType(KeyEvent.VK_N); break;
case 'o': doType(KeyEvent.VK_O); break;
case 'p': doType(KeyEvent.VK_P); break;
case 'q': doType(KeyEvent.VK_Q); break;
case 'r': doType(KeyEvent.VK_R); break;
case 's': doType(KeyEvent.VK_S); break;
case 't': doType(KeyEvent.VK_T); break;
case 'u': doType(KeyEvent.VK_U); break;
case 'v': doType(KeyEvent.VK_V); break;
case 'w': doType(KeyEvent.VK_W); break;
case 'x': doType(KeyEvent.VK_X); break;
case 'y': doType(KeyEvent.VK_Y); break;
case 'z': doType(KeyEvent.VK_Z); break;
case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break;
case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break;
case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break;
case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break;
case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break;
case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break;
case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break;
case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break;
case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break;
case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break;
case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break;
case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break;
case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break;
case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break;
case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break;
case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break;
case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break;
case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break;
case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break;
case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break;
case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break;
case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break;
case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break;
case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break;
case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break;
case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break;
case '`': doType(KeyEvent.VK_BACK_QUOTE); break;
case '0': doType(KeyEvent.VK_0); break;
case '1': doType(KeyEvent.VK_1); break;
case '2': doType(KeyEvent.VK_2); break;
case '3': doType(KeyEvent.VK_3); break;
case '4': doType(KeyEvent.VK_4); break;
case '5': doType(KeyEvent.VK_5); break;
case '6': doType(KeyEvent.VK_6); break;
case '7': doType(KeyEvent.VK_7); break;
case '8': doType(KeyEvent.VK_8); break;
case '9': doType(KeyEvent.VK_9); break;
case '-': doType(KeyEvent.VK_MINUS); break;
case '=': doType(KeyEvent.VK_EQUALS); break;
case '~': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE); break;
case '!': doType(KeyEvent.VK_EXCLAMATION_MARK); break;
case '@': doType(KeyEvent.VK_AT); break;
case '#': doType(KeyEvent.VK_NUMBER_SIGN); break;
case '$': doType(KeyEvent.VK_DOLLAR); break;
case '%': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5); break;
case '^': doType(KeyEvent.VK_CIRCUMFLEX); break;
case '&': doType(KeyEvent.VK_AMPERSAND); break;
case '*': doType(KeyEvent.VK_ASTERISK); break;
case '(': doType(KeyEvent.VK_LEFT_PARENTHESIS); break;
case ')': doType(KeyEvent.VK_RIGHT_PARENTHESIS); break;
case '_': doType(KeyEvent.VK_UNDERSCORE); break;
case '+': doType(KeyEvent.VK_PLUS); break;
case '\t': doType(KeyEvent.VK_TAB); break;
case '\n': doType(KeyEvent.VK_ENTER); break;
case '[': doType(KeyEvent.VK_OPEN_BRACKET); break;
case ']': doType(KeyEvent.VK_CLOSE_BRACKET); break;
case '\\': doType(KeyEvent.VK_BACK_SLASH); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;
case ';': doType(KeyEvent.VK_SEMICOLON); break;
case ':': doType(KeyEvent.VK_COLON); break;
case '\'': doType(KeyEvent.VK_QUOTE); break;
case '"': doType(KeyEvent.VK_QUOTEDBL); break;
case ',': doType(KeyEvent.VK_COMMA); break;
case '<': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_COMMA); break;
case '.': doType(KeyEvent.VK_PERIOD); break;
case '>': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_PERIOD); break;
case '/': doType(KeyEvent.VK_SLASH); break;
case '?': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH); break;
case ' ': doType(KeyEvent.VK_SPACE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
protected void doType(int... keyCodes) {
doTypeChar(keyCodes, 0, keyCodes.length);
}
private void doTypeChar(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doTypeChar(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}
Hope it helps :)
It appeared actually good if I simply wished to print out the weblog article however was not ok if I wished the banners that had been on the weblog. I bumped into the identical issues with the opposite ones too. onlineconvertfree.com
ReplyDelete