Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Rep Power

    Default 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:

    Java Code:
    import java.awt.AWTException;
    import java.awt.Robot;
    public class KeyInputRobot
    	public static void main(String[] args) throws AWTException, IOException, InterruptedException
    		Robot robot=new Robot();
    		BufferedReader input=new BufferedReader(new InputStreamReader(;
    		int time;
    			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.print("Length= ");
    		for(int x=0; x<(2*time); x++)
    Does robot not like function keys or what?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default 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?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Nov 2012
    Rep Power

    Default 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?

Similar Threads

  1. Recursive function to iteractive function
    By mikeZet in forum New To Java
    Replies: 0
    Last Post: 03-13-2012, 02:42 AM
  2. While loop does not function properly
    By Kblavkalash in forum New To Java
    Replies: 3
    Last Post: 05-13-2011, 07:23 PM
  3. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. Replies: 3
    Last Post: 06-06-2008, 12:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts