Results 1 to 3 of 3
- 11-09-2012, 03:29 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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:
Does robot not like function keys or what?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); } } }
- 11-09-2012, 08:37 PM #2
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-09-2012, 11:57 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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
-
Recursive function to iteractive function
By mikeZet in forum New To JavaReplies: 0Last Post: 03-13-2012, 01:42 AM -
While loop does not function properly
By Kblavkalash in forum New To JavaReplies: 3Last Post: 05-13-2011, 06:23 PM -
Calling function in Javascript- from other function
By jdigger in forum New To JavaReplies: 1Last Post: 02-27-2011, 09:00 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
how to pause and repaint in a loop, to draw a mathematical function.
By Abdelhamidem in forum AWT / SwingReplies: 3Last Post: 06-05-2008, 11:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks