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

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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    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?

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

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    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, 01:42 AM
  2. While loop does not function properly
    By Kblavkalash in forum New To Java
    Replies: 3
    Last Post: 05-13-2011, 06:23 PM
  3. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  4. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  5. Replies: 3
    Last Post: 06-05-2008, 11:10 PM

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
  •