[英]How to make Java Robot hold down a key?
我想使用Java機器人按住某個鍵一段時間。 我讀過其他類似的主題,但是它們都不起作用。 反復按該鍵只會導致該鍵不釋放。
到目前為止,這是我的代碼(由於只按了一次鍵,因此無法正常工作):
new Thread(new Runnable() {
public void run() {
final int keyP = 0; //the key to press
final int duration = 2000 //2 seconds
Robot r = null;
try {
r = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
r.keyPress(keyP);
r.delay(duration); //Thread.sleep does the same thing
r.keyRelease(keyP);
}
}).start();
嘗試:
boolean keepPressing=true;
long startTime = System.currentTimeMillis();
long endTime=null;
long totalTime=null;
while(keepPressing){
r.keyPress(keyP);
endTime=System.currentTimeMillis();
totalTime = endTime - startTime;
if(totalTime>1999) {
keepPressing=false;
r.keyRelease(keyP);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.