import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.util.prefs.*;
import javax.swing.JFileChooser;
import java.io.File;
public class MarksKeystroke {
static String key;
static String fullFileName;
static JTextField jtfFile, jtfCell;
static private Robot robot;
static char colNumber;
static JFileChooser chooser ;
static private Preferences prefs;
static String delay;
static String columnBegin="";
static String rowBegin="";
static String columnEnd="";
static String rowEnd ="";
static int columnNo;
static int rowNo = 0;
public static void main(String... args) throws Exception {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setTitle("Marks Entering Keystroke");
f.setSize(450,200);
JPanel titlePanel = new JPanel();
JLabel title = new JLabel("Marks Entering Keystroke");
titlePanel.add(title);
JPanel contentPanel = new JPanel();
//content pane
JPanel contentPanel1 = new JPanel();
contentPanel1.setLayout(new BorderLayout());
//sub content pane 1 for conents.
JPanel cp1 = new JPanel();
cp1.setLayout(new BorderLayout());
JLabel lblFile = new JLabel("File: ");
final JTextField tfPath = new JTextField(20);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println(prefs.get("LAST_OUTPUT_DIR", ""));//<<<<<<<<<<<<<<<
File f = new File ( prefs.get("LAST_OUTPUT_DIR", "")); //<<<<<<<<<<<<<<<
chooser.setCurrentDirectory(f); //<<<<<<<<<<<<<<<
int retval = chooser.showOpenDialog(null);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, process it.
File mostRecentOutputDirectory = chooser.getSelectedFile(); //<<<<<<<<<<<<<<<
prefs.put("LAST_OUTPUT_DIR", mostRecentOutputDirectory.getAbsolutePath());//<<<<<<<<<<<<<<<
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
String fileName = file.getName();
String strDirectory = file.getParent();
String con = "\\";
fullFileName = strDirectory + con + fileName;
tfPath.setText(fullFileName);
System.out.println(fullFileName);
}
}});
cp1.add(lblFile, BorderLayout.WEST);
cp1.add(tfPath, BorderLayout.CENTER);
cp1.add(btnBrowse, BorderLayout.EAST);
JPanel cp2 = new JPanel();
cp2.setLayout(new FlowLayout());
JLabel lblDelay = new JLabel("Delay between Cells: ");
final JTextField tfDelay = new JTextField(5);
JLabel lblType = new JLabel("Move between Cells: ");
CheckboxGroup radio = new CheckboxGroup();
final Checkbox enterButton = new Checkbox("Enter", radio, true);
final Checkbox tabButton = new Checkbox("Tab", radio, false);
cp2.add(lblDelay);
cp2.add(tfDelay);
cp2.add(lblType);
cp2.add(enterButton);
cp2.add(tabButton);
JPanel contentPanel2 = new JPanel();
contentPanel2.setLayout(new BorderLayout());
JPanel cp3 = new JPanel();
JLabel lblCell = new JLabel("Cell Range (A-Z): ");
final JTextField tfCell = new JTextField(5);
cp3.add(lblCell);
cp3.add(tfCell);
JPanel btnPanel = new JPanel();
JButton btnStart = new JButton("Start");
btnPanel.add(btnStart);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
columnBegin=""; rowBegin=""; columnEnd=""; rowEnd =""; //Set to NULL whenever Start button is clicked
delay = tfDelay.getText();
String path = tfPath.getText();
String cell = tfCell.getText();
System.out.println(cell);
int posColon= cell.indexOf(":");
String begin = cell.substring(0, posColon);
String end = cell.substring(posColon+1, cell.length() ) ;
for (int i=0; i<begin.length(); i++)
isNumber(""+begin.substring(i,i+1) ) ;
System.out.println("col is "+columnBegin + "\t row is " + rowBegin );
for (int i=0; i<end.length(); i++)
isNumber1(""+end.substring(i,i+1) ) ;
int rowNo1 = Integer.parseInt(rowBegin) - 1;
int rowNo = Integer.parseInt(rowEnd) ;
System.out.println("col is "+columnEnd + "\t row is " + rowNo );
Character c = new Character(columnBegin.charAt(0));
Character c1 = new Character(columnEnd.charAt(0));
colNumber(c);
colNumber(c1);
try
{
Thread.sleep(1000);
MarksKeystroke keyboard = new MarksKeystroke();
Workbook workbook = Workbook.getWorkbook(new File(path));
Sheet sheet = workbook.getSheet(0);
for (int x=rowNo1; x<rowNo; x++) {
Cell excel1 = sheet.getCell(columnNo, x);
String data1 = excel1.getContents();
System.out.println(data1);
keyboard.type(data1);
if (tabButton.getState() == true) {
keyboard.type("\t");
System.out.println("tab");
} else{
keyboard.type("\n");
System.out.println("enter");
}
//keyboard.type(key);
}
keyboard.type("\n");
}
catch (Exception ex) {}
}
});
contentPanel1.add(cp1, BorderLayout.NORTH);
contentPanel1.add(cp2, BorderLayout.CENTER);
contentPanel2.add(cp3, BorderLayout.NORTH);
contentPanel.add(contentPanel1);
contentPanel.add(contentPanel2);
f.add(titlePanel, BorderLayout.NORTH);
f.add(contentPanel, BorderLayout.CENTER);
f.add(btnPanel, BorderLayout.SOUTH);
f.setVisible(true);
}
static boolean isNumber(String c)
{
try
{
Integer.parseInt(c);
rowBegin +=c;
return true;
}
catch (Exception e)
{
columnBegin +=c;
return false;
}
}
static boolean isNumber1(String c1)
{
try
{
Integer.parseInt(c1);
rowEnd +=c1;
return true;
}
catch (Exception e)
{
columnEnd +=c1;
return false;
}
}
static void colNumber (char character)
{
// int columnNo = 0;
switch (character)
{
case 'A': columnNo =0; break;
case 'B': columnNo = 1; break;
case 'C': columnNo = 2; break;
case 'D': columnNo = 3; break;
case 'E': columnNo = 4; break;
case 'F': columnNo = 5; break;
case 'G': columnNo = 6; break;
case 'H': columnNo = 7; break;
case 'I': columnNo = 8; break;
case 'J': columnNo = 9; break;
case 'K': columnNo = 10; break;
case 'L': columnNo = 11; break;
case 'M': columnNo = 12; break;
case 'N': columnNo = 13; break;
case 'O': columnNo = 14; break;
case 'P': columnNo = 15; break;
case 'Q': columnNo = 16; break;
case 'R': columnNo = 17; break;
case 'S': columnNo = 18; break;
case 'T': columnNo = 19; break;
case 'U': columnNo = 20; break;
case 'V': columnNo = 21; break;
case 'W': columnNo = 22; break;
case 'X': columnNo = 23; break;
case 'Y': columnNo = 24; break;
case 'Z': columnNo = 25; break;
}
}
public MarksKeystroke() throws AWTException {
this.robot = new Robot();
prefs = Preferences.userNodeForPackage(this.getClass());
}
public MarksKeystroke(Robot robot) {
this.robot = robot;
}
public void type(CharSequence characters) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
}
public void type(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case 'e': doType(KeyEvent.VK_E); break;
case 'f': doType(KeyEvent.VK_F); break;
case 'g': doType(KeyEvent.VK_G); break;
case 'h': doType(KeyEvent.VK_H); break;
case 'i': doType(KeyEvent.VK_I); break;
case 'j': doType(KeyEvent.VK_J); break;
case 'k': doType(KeyEvent.VK_K); break;
case 'l': doType(KeyEvent.VK_L); break;
case 'm': doType(KeyEvent.VK_M); break;
case 'n': doType(KeyEvent.VK_N); break;
case 'o': doType(KeyEvent.VK_O); break;
case 'p': doType(KeyEvent.VK_P); break;
case 'q': doType(KeyEvent.VK_Q); break;
case 'r': doType(KeyEvent.VK_R); break;
case 's': doType(KeyEvent.VK_S); break;
case 't': doType(KeyEvent.VK_T); break;
case 'u': doType(KeyEvent.VK_U); break;
case 'v': doType(KeyEvent.VK_V); break;
case 'w': doType(KeyEvent.VK_W); break;
case 'x': doType(KeyEvent.VK_X); break;
case 'y': doType(KeyEvent.VK_Y); break;
case 'z': doType(KeyEvent.VK_Z); break;
case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break;
case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break;
case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break;
case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break;
case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break;
case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break;
case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break;
case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break;
case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break;
case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break;
case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break;
case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break;
case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break;
case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break;
case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break;
case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break;
case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break;
case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break;
case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break;
case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break;
case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break;
case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break;
case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break;
case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break;
case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break;
case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break;
case '`': doType(KeyEvent.VK_BACK_QUOTE); break;
case '0': doType(KeyEvent.VK_0); break;
case '1': doType(KeyEvent.VK_1); break;
case '2': doType(KeyEvent.VK_2); break;
case '3': doType(KeyEvent.VK_3); break;
case '4': doType(KeyEvent.VK_4); break;
case '5': doType(KeyEvent.VK_5); break;
case '6': doType(KeyEvent.VK_6); break;
case '7': doType(KeyEvent.VK_7); break;
case '8': doType(KeyEvent.VK_8); break;
case '9': doType(KeyEvent.VK_9); break;
case '-': doType(KeyEvent.VK_MINUS); break;
case '=': doType(KeyEvent.VK_EQUALS); break;
case '~': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE); break;
case '!': doType(KeyEvent.VK_EXCLAMATION_MARK); break;
case '@': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_2); break;
//case '@': doType(KeyEvent.VK_AT); break;
case '#': doType(KeyEvent.VK_NUMBER_SIGN); break;
case '$': doType(KeyEvent.VK_DOLLAR); break;
case '%': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5); break;
case '^': doType(KeyEvent.VK_CIRCUMFLEX); break;
case '&': doType(KeyEvent.VK_AMPERSAND); break;
case '*': doType(KeyEvent.VK_ASTERISK); break;
case '(': doType(KeyEvent.VK_LEFT_PARENTHESIS); break;
case ')': doType(KeyEvent.VK_RIGHT_PARENTHESIS); break;
case '_': doType(KeyEvent.VK_UNDERSCORE); break;
case '+': doType(KeyEvent.VK_PLUS); break;
case '\t': doType(KeyEvent.VK_TAB); break;
case '\n': doType(KeyEvent.VK_ENTER); break;
case '[': doType(KeyEvent.VK_OPEN_BRACKET); break;
case ']': doType(KeyEvent.VK_CLOSE_BRACKET); break;
case '\\': doType(KeyEvent.VK_BACK_SLASH); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;
case ';': doType(KeyEvent.VK_SEMICOLON); break;
case ':': doType(KeyEvent.VK_COLON); break;
case '\'': doType(KeyEvent.VK_QUOTE); break;
case '"': doType(KeyEvent.VK_QUOTEDBL); break;
case ',': doType(KeyEvent.VK_COMMA); break;
case '<': doType(KeyEvent.VK_LESS); break;
case '.': doType(KeyEvent.VK_PERIOD); break;
case '>': doType(KeyEvent.VK_GREATER); break;
case '/': doType(KeyEvent.VK_SLASH); break;
case '?': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH); break;
case ' ': doType(KeyEvent.VK_SPACE); break;
//Arrow Key
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
public void typeArrow(String d) {
if (d.equals("Up")) doType(KeyEvent.VK_UP); else
if (d.equals("Down")) doType(KeyEvent.VK_DOWN); else
if (d.equals("Left")) doType(KeyEvent.VK_LEFT); else
if (d.equals("Right")) doType(KeyEvent.VK_RIGHT);
}
private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.delay(Integer.parseInt(delay));
robot.keyRelease(keyCodes[offset]);
}
}