Using function keys with robot within a loop
I am trying to create a program that uses the robot constructor to hit a f9 every 30 seconds, but it only seems to do it either once or none at all (I am testing it with other programs like Minecraft and Fraps to test the function keypresses). However, when I type in a keycode for a character it loops it correctly. I manually hit the function keys to make sure that the two aforesaid programs would accept those keypresses within a short time frame, and it worked.
Here is the code from Eclipse:
Code:
import java.awt.AWTException;
import java.awt.Robot;
import java.io.*;
public class KeyInputRobot
{
public static void main(String[] args) throws AWTException, IOException, InterruptedException
{
Robot robot=new Robot();
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int time;
do
{
System.out.println("Please input the time of the video to be recorded (rounded up to the next minute).");
System.out.println("If you want to continuously record until you terminate this program, please type 0.");
System.out.println("Please note that there will be a three second delay between entering the time, and the first execution.");
System.out.println("The delay is intended to give you time to get ready.");
System.out.println();
System.out.print("Length= ");
time=Integer.parseInt(input.readLine());
}while(time<=0);
Thread.currentThread();
Thread.sleep(3000);
for(int x=0; x<(2*time); x++)
{
robot.delay(1000);
robot.keyPress(120);
System.out.println("Pressed");
robot.delay(30000);
}
}
}
Does robot not like function keys or what?
Re: Using function keys with robot within a loop
1. Use a Timer, not Thread.sleep(...) in a loop, for repeating a code segment.
2. Avoid magic numbers. See the documentation for Robot#keyPress(...) for an example of how you should pass the int parameter.
3. Where is the key released so that it can be pressed again?
db
Re: Using function keys with robot within a loop
The problem did seem to be that the key was never released. I never though to release the key, because when robot presses a character, it only outputs the character once, and not many times as if the key was being held down. Why doesn't it act like the key is being held down if you never tell robot to release the key?