-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPressAkeyCommand.java
59 lines (50 loc) · 1.2 KB
/
PressAkeyCommand.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
package Keyboard;
import java.awt.AWTException;
import java.awt.Robot;
/**
* 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
*/
class PressAKeyCommand{
public PressAKeyCommand(int key){
this.key = key;
}
public PressAKeyCommand(int key,long time){
this.key = key;
this.time = time;
}
private int key;
private long time = 0;
private volatile boolean isContinue = true;
public void execute() {
try {
Robot robot = new Robot();
if(time == 0){
while (isContinue) {
robot.keyPress(key);
System.out.println(isContinue);
}
robot.keyRelease(key);
}
else{
long curTime = System.currentTimeMillis();
while (isContinue) {
while (System.currentTimeMillis() - curTime < time) {
robot.keyPress(key);
}
stop();
}
robot.keyRelease(key);
}
} catch (AWTException ex) {
ex.printStackTrace();
}
}
public void stop() {
isContinue = false;
}
}