Currently i'm trying to have java hold down a key like follows:
Robot rob;
rob.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000);
rob.keyRelease(KeyEvent.VK_ENTER);
This should hold enter down for 3 seconds, causing the repeating effect after a second or so. In other words, if you were to manually hold the "r" key, it would first type r, and then after about a second it would go like rrrrrrrr. I want this effect from the robot. I also tried:
curTime = System.currentTimeMillis();
while(System.currentTimeMillis() - curTime < duration)
{
rob.keyPress(whatever);
}
rob.keyRelease(whatever);
This, however, is extremely sensitive and a duration of 1 second outputs... well, as many whatever's as your computer can in 1 second. Thousands of lines worth. This is not my intention. Any ideas? Thanks!
P.S. The reason I want this behavior is because im writing a little scripting language to automate games with. If I want to hold the up arrow key like a normal person, I think that I need the behavior i'm talking about.
Edit:
Since there seems to be some confusion, I appologize. Let me elaborate. In my first code peice, if I choose "r" to be the character, it will just print ONE r regardless of the duration. If you, on your keyboard, press "r" for 5 seconds, it will go -> r...rrrrrrrrrrrrrrr where ... means like a second of time. That is the behavior I want, but I wont get it. The second code is where I try to spam click "press", but this literally types "r" EVERY time it executes. So if I am in a timed loop for a duration, every time that loop iterates it will send the "r" button. That's not what I want. What I want, again , is the same result that would happen as if you pushed r down on your keyboard for 3 seconds. First its just one r, and then rrrrrrrrrrrrrrrrrrrrr. I'm not even sure what the release() method does... I figured if you left it on press without release, it would just SPAM the screen in a loop! Why wouldnt it, the key is PRESSED? This is what is confusing me. Apparently when a key is pressed it doesnt STAY pressed.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…