Thread.sleep()은 현재 스레드 (키를 누르고있는 스레드)의 실행을 중지합니다.
주어진 시간 동안 키를 누르고 싶으면 병렬 스레드에서 실행해야합니다. 여기
는 Thread.sleep를() 문제를 해결 얻을 것이다 제안입니다 (다른 명령을 만들고 마음대로과 그들을 교환 할 수 있도록 명령 패턴을 사용) :
public class Main {
public static void main(String[] args) throws InterruptedException {
final RobotCommand pressAKeyCommand = new PressAKeyCommand();
Thread t = new Thread(new Runnable() {
public void run() {
pressAKeyCommand.execute();
}
});
t.start();
Thread.sleep(5000);
pressAKeyCommand.stop();
}
}
class PressAKeyCommand implements RobotCommand {
private volatile boolean isContinue = true;
public void execute() {
try {
Robot robot = new Robot();
while (isContinue) {
robot.keyPress(KeyEvent.VK_A);
}
robot.keyRelease(KeyEvent.VK_A);
} catch (AWTException ex) {
// Do something with Exception
}
}
public void stop() {
isContinue = false;
}
}
interface RobotCommand {
void execute();
void stop();
}
네가 맞아. 온라인으로 java 문서를 보면 keyPress가 키를 누르는 데 사용되며 keyRelease가 동일한 키를 릴리스하는 데 사용됩니다. 나는이 문제가 thread.sleep()과 같을 것이라고 생각한다. –