Sunday, November 27, 2011

Further modifying the Robot program to take the input from properties file in a specific format:

[1] TestRobot.java

import java.awt.AWTException;
import java.awt.Robot;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.swing.KeyStroke;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author PD
 * This program will execute set of mouse key events repeatedly after a sleep.
 * Keys input will be taken from the properties file in format: [PRESS|RELEASE]::[PRESS|RELEASE]:
 * E.g. PRESS:A:RELEASE:A
 * Press denotes Keyboard KeyPress event where as RELEASE dentoes KeyRelease event.
 * Above example will press and release keyboard key A after every value mentioned in properties file [default is 1 min].
 */
public class TestRobot {
private static enum Key{PRESS, RELEASE};

    private static final int DEFAULT_SLEEP_TIME_IN_MILLISEC = 10000; // 60000 milliseconds = 1 min

    public static void main(String[] args) throws AWTException, IOException 
    {
    long startTime = new Date().getTime();
        Robot r = new Robot();
        Properties p = new Properties();
        int sleepTime = 0;
        int endTime = 0;
        String[] keyArray = null;

        try {
            p.load(new FileInputStream("live.properties"));
            sleepTime = Integer.parseInt(p.getProperty("wakeup.time"));
            System.out.println("sleepTime = " + sleepTime);
            if(sleepTime == 0)
            sleepTime = DEFAULT_SLEEP_TIME_IN_MILLISEC;
            endTime = Integer.parseInt(p.getProperty("execution.time"));
            System.out.println("Execution Time : " + endTime);
            String keys = p.getProperty("keys");

            if(keys != null) {
            if(!Pattern.matches("^((PRESS|RELEASE):\\w+:)+(PRESS|RELEASE):\\w+$", keys))
            throw new Exception("INVALID keys. Expecting value for keys in properties file in format '[PRESS|RELEASE]::[PRESS|RELEASE]:' i.e. PRESS or RELEASE must followed by a key.\nE.g. for ALT+S provide keys = PRESS:ALT:PRESS:S:RELEASE:S:RELEASE:ALT");

            keyArray = keys.split(":");
             if(keyArray == null)
            throw new Exception("INVALID keys. Expecting value for keys in properties file in format '[PRESS|RELEASE]::[PRESS|RELEASE]:' i.e. PRESS or RELEASE must followed by a key.\nE.g. for ALT+S provide keys = PRESS:ALT:PRESS:S:RELEASE:S:RELEASE:ALT");
            }
          
       while(true) {
           try {
            Thread.sleep(sleepTime);
           } catch (InterruptedException e1) {
               break;
           }
           for (int i = 0; i < keyArray.length; i+=2) {
            Key key = Key.valueOf(keyArray[i]);

            switch (key) {
case PRESS:
r.keyPress(KeyStroke.getKeyStroke(keyArray[i+1]).getKeyCode());
break;
case RELEASE:
r.keyRelease(KeyStroke.getKeyStroke(keyArray[i+1]).getKeyCode());
break;
default:
throw new Exception("INVALID keys. Expecting value for keys in properties file in format '[PRESS|RELEASE]::[PRESS|RELEASE]:' i.e. PRESS or RELEASE must followed by a key.\nE.g. for ALT+S provide keys = PRESS:ALT:PRESS:S:RELEASE:S:RELEASE:ALT");

}
}
            if(endTime <= 0 && (startTime + endTime) < new Date().getTime()) {
            System.exit(0);
           }
       }
    } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
    }
        
    }

}

[2] live.properties

# Time is in miliseconds & execution.time <= 0 is infinite
execution.time = 1200000
wakeup.time = 240000
# keys format '[PRESS|RELEASE]:' E.g. for ALT+S provide keys = PRESS:ALT:PRESS:S:RELEASE:S:RELEASE:ALT
keys = PRESS:ALT:PRESS:TAB:RELEASE:TAB:PRESS:SHIFT:PRESS:TAB:RELEASE:TAB:RELEASE:SHIFT:RELEASE:ALT

Note:
With above values of properties file, program will execute keys repetitively after every 4 min for total 20 min.   keys are "ALT+TAB" followed by "ALT+SHIFT+TAB".

No comments: