-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKeyboard.java
370 lines (344 loc) · 14.5 KB
/
Keyboard.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package Keyboard;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
/**
* A simple Library for controlling the Keyboard in Java.
* License : Mozilla Public License 2.0 / http://choosealicense.com/licenses/mpl-2.0/#
*
* @author AYIDouble / https://github.com/AYIDouble
* @version 1.0
* @since 22.02.2016
*/
public class Keyboard {
/**
* you can use the methods of the robot and access his attributes
*/
public Robot robot; //public for adjustments on the robot
/**
* robot of the class will be created at initalize
*/
public Keyboard() throws AWTException{
robot = new Robot();
}
/**
* initalize Keyboard with your own robot
* Otherwise just initalize it with the no parameters
*
* @param robot - use the robot of you class
*/
public Keyboard(Robot robot) {
this.robot = robot;
}
/**
* @param key - write the Charcode of the Key you want to press, example "p" : 80
*/
public void press(char key){
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(key));
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(key));
}
/**
* @param text - write the Keys you want to be pressed, example : "hello"
*/
public void press(String text){
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
type(keys[i]);
}
}
/**
* Write a text , it's possible to Write Uppercase
*
* Example : "Hello" : 5 type 'H',wait 5 milliseconds,type 'E',wait 5 milliseconds ...
*
* @param text - write the Keys you want to be pressed, example : "hello"
* @param speed - milliseconds before writing the next Key, example : 5
*/
public void press(String text,long speed) throws InterruptedException{ // you can define how fast the text should be typed [speed = milliseconds][1000 milliseconds = 1 second]
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
type(keys[i]);
Thread.sleep(speed);
}
}
/**
* Press Keys and keep the Keys pressed,you can use release() for releasing the Keys
*
* @param key - write the Charcode of the Key you want to be pressed, example "p" : 80
*/
public void press(int key){
robot.keyPress(key);
robot.keyRelease(key);
}
/**
* Press Keys and release them example Windows Key + O , to change Monitor Settings.
*
* @param keys - write the Charcodes of the Key you want to be pressed, example {KeyEvent.VK_WINDOWS,KeyEvent.VK_O}
* @throws AWTException
*/
public void pressRelease(ArrayList<Integer> keys) throws AWTException{
for (int i = 0; i < keys.size(); i++) {
System.out.println(keys.get(i));
executePress(keys.get(i));
}
}
/**
* Press Keys and release them example Windows Key + O , to change Monitor Settings.
*
* @param keys - write the Charcodes of the Key you want to be pressed, example {KeyEvent.VK_WINDOWS,KeyEvent.VK_O}
* @param time - How many many milliseconds should the keys be pressed 1000 = 1 second
* @throws InterruptedException
* @throws AWTException
*/
public void pressRelease(ArrayList<Integer> keys,long time) throws InterruptedException, AWTException{
for (int i = 0; i < keys.size(); i++) {
System.out.println(keys.get(i));
executePress(keys.get(i),time);
}
}
/**
* This is good if you want to type a lowercase text
* Otherwise use type()
*
* @param text - write the text you want to be typed in lowercase, example : "hello"
*/
public void lowerCasePress(String text){
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i]));
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(keys[i]));
}
}
/**
* Press Key and keep the Key pressed,you can use release() for releasing the Key
*
* @param key - write the Key you want to be pressed, example : 'a'
*/
public void keepPressed(char key){
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(key));
}
/**
* Press Keys and keep the Keys pressed,you can use release() for releasing the Keys
*
* @param text - write the Key you want to be pressed, example : "aswd"
*/
public void keepPressed(String text){
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i]));
}
}
/**
* Press Key and keep the Key pressed,you can use release() for releasing the Keys
*
* @param key - write the Key you want to be pressed, example "p" : 80
*/
public void keepPressed(int key){
robot.keyPress(key);
}
/**
* hold these Keys down, with speed attribute that means it will wait x milliseconds before it keep pressed the other Key.
*
* Example : "Hello" : 5 keep Pressed 'H',wait 5 milliseconds,keep Pressed 'E',wait 5 milliseconds ...
*
* @param text - write the Keys you want to be keep pressed, example : "hello"
* @param speed - milliseconds before keeping pressed the next Key, example : 5
*/
public void keepPressed(String text,long speed) throws InterruptedException{ // you can define how fast the text should be typed [speed = milliseconds][1000 milliseconds = 1 second]
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i]));
Thread.sleep(speed);
}
}
/**
* use release() for releasing the Keys
*
* @param key - write the Key you want to be released, example "p" : 'p'
*/
public void release(char key){
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(key));
}
/**
* use release() for releasing the Keys
*
* @param text - write the Text you want to be released, example "hello", the Keys will be released when they were pressed
*/
public void release(String text){
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(keys[i]));
}
}
/**
* use release() for releasing the Keys
*
* @param key - write the Key you want to be released, example "p" : 80
*/
public void release(int key){
robot.keyRelease(key);
}
/**
* release these Keys, with speed attribute that means it will wait x milliseconds before it releases the other Key.
*
* Example : "Hello" : 5 release 'H',wait 5 milliseconds,release 'E',wait 5 milliseconds ...
*
* @param text - write the Keys you want to be keep pressed, example : "hello"
* @param speed - milliseconds before releasing the next Key, example : 5
* @throws InterruptedException
*/
public void release(String text,long speed) throws InterruptedException{ // you can define how fast the text should be typed [speed = milliseconds][1000 milliseconds = 1 second]
char[] keys = text.toCharArray();
for (int i = 0; i < keys.length; i++) {
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i]));
Thread.sleep(speed);
}
}
/**
* Important! This will work with uppercase Text and you should use it if you want to type a uppercase Text.
*
* @param character - write the Key or Text you want to be typed, example "Type This" or 'A'
*/
public void type(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:
try{
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(character));
}catch(Exception e){
throw new IllegalArgumentException("Cannot type character " + character);
}
}
}
/**
* Important! This will work with uppercase Text and you should use it if you want to type a uppercase Text.
*
* @param characters - write the Key or Text you want to be typed, example "Type This" or 'A'
*/
public void type(CharSequence characters) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
}
private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}else{
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}
//Presses a key for a amount of time
private void executePress(int keyEvent,long duration) throws AWTException {
PressAKeyCommand pakc = new PressAKeyCommand(keyEvent,duration);
pakc.execute();
}
private void executePress(int keyEvent) throws AWTException {
PressAKeyCommand pakc = new PressAKeyCommand(keyEvent,10);
pakc.execute();
pakc.stop();
}
}