Results 1 to 7 of 7
  1. #1
    weezypenguin is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default help with a problem please?

    I have never really tried java before.
    So i am wondering if this is possible with a java app or script.
    I want it to open a particular application,
    then send a ctl+c or F1 key command (this can be configured within the application).
    Then wait wait a given amount of time and then close the application.
    But the kicker is the machine is virtual and so has no actual keyboard, mouse or monitor etc.
    So if anyone could let me know if this possible that would be great.
    Thanks in advance

    PS if java is not the way to do it can anyone direct me please, i have been trying to fix this for months.
    Thanks

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I would say Java is not the best way to do this, a scripting language would be better, but it IS possible. If you are on Windows, I know that AutoHotkey is capable of doing what you want.

    In Java, you can use the ProcessBuilder class to run a program. Then, use the Robot class to send the keystrokes. Finally, you can close your run program by invoking process.destroy on the Process object returned by processBuilder.start.

    Java Code:
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.io.IOException;
    
    public class RunProg
    {
    	/**
    	 * Main function
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		// Run notepad
    		String pathName = "notepad.exe";
    		ProcessBuilder processBuilder = new ProcessBuilder(pathName);
    		Process process;
    
    		try {
    			process = processBuilder.start();
    		} catch (IOException e) {
    			e.printStackTrace();
    			return;
    		}
    
    		Robot robot;
    		try {
    			robot = new Robot();
    		} catch (AWTException e) {
    			e.printStackTrace();
    			return;
    		}
    
    		// Send F1
    		robot.keyPress(KeyEvent.VK_F1);
    		robot.keyRelease(KeyEvent.VK_F1);
    
    		// Sleep for 3 seconds
    		robot.delay(3000);
    
    		// Close the launched program
    		process.destroy();
    	}
    }
    Last edited by CodesAway; 12-08-2009 at 06:33 AM. Reason: Provided a more complete example
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    weezypenguin is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply.
    I haqve tried autohot key but struggled with as the script runs on a the PC that can only be accessed by RDP session, so no actual board or mouse. And it has to do it when the session is not running.
    Any help me is appricated
    Thanks

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Does the code I provided work as needed? If not, why not?

    In order to give a working solution, I need more details - including what doesn't work, why not, and what you were expecting.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    weezypenguin is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply.
    I have not tried the code just yet.
    What is it supposed to do? I am sorry to say that i am not the java code writer it is a friend of mine and i can not get hold of him tonight.
    I will send it hime to see if he has tire dthis in his code.
    So am i right in understanding the ROBOt will actual do the keyboard presses, and can do this while the system is logged off?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by weezypenguin View Post
    Thanks for the reply.
    I have not tried the code just yet.
    What is it supposed to do? I am sorry to say that i am not the java code writer it is a friend of mine and i can not get hold of him tonight.
    I will send it hime to see if he has tire dthis in his code.
    So am i right in understanding the ROBOt will actual do the keyboard presses, and can do this while the system is logged off?
    Right, the Robot class will actually perform a key press just like if a user pressed it.

    I'm not sure what you mean by "while the system is logged off". If you are referring to the RDP, then yes, it should work. The robot class can emulate keystrokes, without requiring user interaction.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM

Posting Permissions

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