Making a Java Robot
A robot is basically a program,which can do something on its own,but its guided by the programmer.Using Java you can also create your own robot program that will perform some activity automatically. Here we came up with a very interesting Program. Lets develop a simple Robot in Java that will
- launch a AWT Form Page
- takes the control over mouse and automatically moves the cursor to form components
- blinks the caps lock for specific period of time
- types automatically in the username and password field
- and submits the form
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
| import java.awt.*;import java.awt.event.*;import javax.swing.*;public class RobotDemo implements ActionListener { JFrame fr; JLabel usernameLabel; JLabel passwordLabel; JTextField usernameText; JTextField passwordText; JButton loginBtn; public RobotDemo() throws AWTException { fr = new JFrame(); usernameLabel = new JLabel("userName"); passwordLabel = new JLabel("Password"); usernameText = new JTextField(10); passwordText = new JTextField(10); loginBtn = new JButton("Login"); loginBtn.addActionListener(this); fr.add(usernameLabel); fr.add(usernameText); fr.add(passwordLabel); fr.add(passwordText); fr.add(loginBtn); 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 (InterruptedException e) {} } for (int i = 1; i < 10; i++) { rob.keyPress(KeyEvent.VK_CAPS_LOCK); try {Thread.sleep(500);} catch (InterruptedException e) {} rob.keyRelease(KeyEvent.VK_CAPS_LOCK); } int keyCodes[] = new int[] { KeyEvent.VK_N, KeyEvent.VK_A, KeyEvent.VK_T, KeyEvent.VK_A, KeyEvent.VK_R, KeyEvent.VK_A, KeyEvent.VK_Z }; int key; for (int i = 0; i < keyCodes.length; i++) { key = keyCodes[i]; rob.keyPress(key); rob.keyRelease(key); try {Thread.sleep(500);} catch (InterruptedException e) {} } for (int i = 35; i < 38; i++) { rob.mouseMove(i * 10, i * 10); try {Thread.sleep(50);} catch (InterruptedException 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 (InterruptedException 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 ae) { if (ae.getSource() == loginBtn) { JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE); } }} |
The output of the above program would be the below form page,which is completely operated by program and not by human.
Just execute the program to see the robot working for you. Hope You liked this article.Happy roboting…..
No comments:
Post a Comment