Skip to content

Instantly share code, notes, and snippets.

@jpt1122
Created September 21, 2013 06:50
Show Gist options
  • Save jpt1122/6647932 to your computer and use it in GitHub Desktop.
Save jpt1122/6647932 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RobotDemo implements ActionListener {
JFrame fr;
JLabel lb1;
JLabel lb2;
JTextField tf1;
JTextField tf2;
JButton btn1;
public RobotDemo() throws AWTException {
fr = new JFrame();
lb1 = new JLabel("userName");
lb2 = new JLabel("Password");
tf1 = new JTextField(10);
tf2 = new JTextField(10);
btn1 = new JButton("Login");
btn1.addActionListener(this);
fr.add(lb1);
fr.add(tf1);
fr.add(lb2);
fr.add(tf2);
fr.add(btn1);
fr.setLayout(new FlowLayout());
fr.setVisible(true);
fr.setSize(200, 400);
fr.setResizable(false);
fr.setLocation(200, 300);
Robot rob = new Robot(); //throws AWTException
//Moves the cursor from 10,10 to 350,350
for (int i = 1; i < 35; i++) {
rob.mouseMove(i * 10, i * 10);
try {
Thread.sleep(50);
} catch (Exception e) {}
}
for (int i = 1; i < 10; i++) {
rob.keyPress(KeyEvent.VK_CAPS_LOCK);
try {
Thread.sleep(500);
} catch (Exception e) {}
rob.keyRelease(KeyEvent.VK_CAPS_LOCK);
}
int keyCodes[] = new int[] {
KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_T, KeyEvent.VK_Y, KeyEvent.VK_A
};
int key;
for (int i = 0; i < keyCodes.length; i++) {
key = keyCodes[i];
rob.keyPress(key);
rob.keyRelease(key);
try {
Thread.sleep(500);
} catch (Exception e) {}
}
for (int i = 35; i < 38; i++) {
rob.mouseMove(i * 10, i * 10);
try {
Thread.sleep(50);
} catch (Exception e) {}
}
//clicks mouse left button,so that we can get focus in pwd textfield
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
keyCodes = new int[] {
KeyEvent.VK_T, KeyEvent.VK_E, KeyEvent.VK_C, KeyEvent.VK_H
};
for (int key1: keyCodes) {
rob.keyPress(key1);
rob.keyRelease(key1);
try {
Thread.sleep(500);
} catch (Exception e) {}
}
rob.mouseMove(300, 400);
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
public static void main(String[] args) throws AWTException {
new RobotDemo();
}
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment