-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
321 lines (244 loc) · 9.22 KB
/
Main.java
File metadata and controls
321 lines (244 loc) · 9.22 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Version 1.1
package me.AustinNotAustin.AustinsAutoClicker;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
public class Main extends JFrame implements ActionListener {
// interval is the time between actions during operation in milliseconds
static int interval;
// Create each new object in the GUI
// frame = the main GUI window
static JFrame frame = new JFrame();
// mainPanel = the inner pane of the window
static JPanel mainPanel = new JPanel();
// start/stop panel
static JPanel SSPanel = new JPanel();
// delay between actions panel
static JPanel intervalPanel = new JPanel();
// repetition panel
static JPanel repPanel = new JPanel();
// notification panel
static JPanel notiPanel = new JPanel();
// startBtn = the button to start the program
static JButton startBtn = new JButton();
// stopBtn = the button to stop the program
static JButton stopBtn = new JButton();
// intervalFld = the text box for the user to enter their
// interval time between actions
static JTextField intervalFld = new JTextField();
// intervalLbl = the text to show above the intervalFld
static JLabel intervalLbl = new JLabel();
// secondsRBtn = the seconds radio button for intervals
static JRadioButton secondsRBtn = new JRadioButton();
// milisecondsRBTn = the milliseconds radio button for intervals
static JRadioButton milisecondsRBtn = new JRadioButton();
// btnGroup = the group to place radio buttons in for interval selection
static ButtonGroup btnGroup = new ButtonGroup();
// notificationLbl = the string of text at the bottom to speak to the user
static JLabel notificationLbl = new JLabel();
// repetitionLbl = the text to ask for number of repetitions
static JLabel repetitionLbl = new JLabel();
// repetitionFld = the text field for the number of repetitions to click/press
static JTextField repetitionFld = new JTextField();
// continuousLbl = the text to ask to run continuously or not
static JLabel continuousLbl = new JLabel();
// continuousBox = the check box where the user can determine if they want to run continuously or not
static JCheckBox continuousBox = new JCheckBox();
// Create the key listener
static KeyListener keyListener = new KeyListener();
// Create the variable for the number of iterations/repetitions
static int repetitions = 0;
public static void main(String args[]) throws AWTException {
Main main = new Main();
main.createGUI();
System.out.println("Testerino");
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
System.err.println("Unable to register native hook from KeyListener");
System.err.println(e.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new KeyListener());
}
// Create the GUI objects
public void createGUI() {
// Set the frame's attributes
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Auto Clicker");
frame.setVisible(true);
frame.setSize(400,220);
frame.setLocation(500, 300);
// Set the start button's attributes
startBtn.setText("Start (F9)");
startBtn.setActionCommand("start");
startBtn.setMnemonic(KeyEvent.VK_F9);
// Set the stop button's attributes
stopBtn.setText("Stop (F10)");
stopBtn.setActionCommand("stop");
stopBtn.setMnemonic(KeyEvent.VK_F10);
// Register a listener to the start and stop buttons
startBtn.addActionListener(this);
stopBtn.addActionListener(this);
// Set the interval field's attributes
intervalFld.setColumns(6);
intervalFld.setActionCommand("intervalFld");
// Set the interval label's attributes
intervalLbl.setText("Interval: ");
// Set the radio button attributes
secondsRBtn.setText("Seconds");
secondsRBtn.setActionCommand("seconds");
milisecondsRBtn.setText("Miliseconds");
milisecondsRBtn.setActionCommand("miliseconds");
milisecondsRBtn.setSelected(true);
// Set the continuous vs repetition attributes
repetitionLbl.setText("Repetitions:");
repetitionFld.setColumns(6);
continuousLbl.setText("Continuous | Off");
continuousLbl.setEnabled(false);
continuousBox.setActionCommand("toggleContinuous");
continuousBox.addActionListener(this);
// Group the radio buttons together
btnGroup.add(secondsRBtn);
btnGroup.add(milisecondsRBtn);
// Add the objects to the panels
intervalPanel.add(milisecondsRBtn);
intervalPanel.add(secondsRBtn);
intervalPanel.add(intervalLbl);
intervalPanel.add(intervalFld);
repPanel.add(continuousBox);
repPanel.add(continuousLbl);
repPanel.add(repetitionLbl);
repPanel.add(repetitionFld);
SSPanel.add(startBtn);
SSPanel.add(stopBtn);
notiPanel.add(notificationLbl);
// Configure the panels
intervalPanel.setBorder(BorderFactory.createEtchedBorder());
repPanel.setBorder(BorderFactory.createEtchedBorder());
// Add the panels to the frame
mainPanel.add(intervalPanel);
mainPanel.add(repPanel);
mainPanel.add(SSPanel);
mainPanel.add(notiPanel);
frame.add(mainPanel);
}
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("actionPerformed() was just called.");
if (event.getActionCommand() == "start") {
System.out.println("start event called");
callStart();
}
if (event.getActionCommand() == "stop") {
System.out.println("stop event called");
// Enable all objects
// Stop the program
stopProgram();
}
// If the command is toggle continuous, call the function
if (event.getActionCommand() == "toggleContinuous")
toggleContinuous();
}
public static int intervalTime() {
// Set the newInterval int to 0 by default
int newInterval = 0;
// Try to convert the string into an integer
try {
// Set the newInterval to the users entered value
newInterval = Integer.parseInt(intervalFld.getText());
System.out.println("The try statement grabs the value of newInterval at " + newInterval);
// If it's accepted so far, remove any message that may be present
notificationLbl.setText("");
} catch (Exception e) {
// If an exception is thrown, send the user this message
notificationLbl.setText("Please enter a number in the interval field");
System.out.println("Exception in the intervalTime() " + e);
return 0;
}
// If the seconds radio button is selected, multiply the currently set interval
if (secondsRBtn.isSelected()) {
// Multiply the interval seconds by 1000 since the time in here is in milliseconds
newInterval *= 1000;
}
// If the milliseconds radio button is selected, we don't make any changes
return newInterval;
}
public static void stopProgram() {
StartProgram.shutdown = true;
// Enable objects once the program stops
enableObjects();
}
// Enables objects while the program is NOT running
public static void enableObjects() {
startBtn.setEnabled(true);
secondsRBtn.setEnabled(true);
milisecondsRBtn.setEnabled(true);
intervalLbl.setEnabled(true);
intervalFld.setEnabled(true);
continuousBox.setEnabled(true);
if (isContinuous()) {
continuousLbl.setEnabled(true);
}
else if (!isContinuous()) {
repetitionLbl.setEnabled(true);
repetitionFld.setEnabled(true);
}
}
// Disables objects while the program is running
public static void disableObjects() {
System.out.println("disableObjects() was just called in iteself");
startBtn.setEnabled(false);
secondsRBtn.setEnabled(false);
milisecondsRBtn.setEnabled(false);
intervalLbl.setEnabled(false);
intervalFld.setEnabled(false);
continuousBox.setEnabled(false);
if (isContinuous()) {
continuousLbl.setEnabled(false);
}
else if (!isContinuous()) {
repetitionLbl.setEnabled(false);
repetitionFld.setEnabled(false);
}
}
public static Boolean isContinuous() {
// Returns if the check box is selected or not
return continuousBox.isSelected();
}
public static void toggleContinuous() {
if (continuousBox.isSelected()) {
// Disable the repetition portion
repetitionLbl.setEnabled(false);
repetitionFld.setEnabled(false);
continuousLbl.setEnabled(true);
continuousLbl.setText("Continuous | On");
}
if (!(continuousBox.isSelected())) {
// Disable the repetition portion
repetitionLbl.setEnabled(true);
repetitionFld.setEnabled(true);
continuousLbl.setEnabled(false);
continuousLbl.setText("Continuous | Off");
}
}
public static void callStart() {
StartProgram.shutdown = false;
(new Thread(new StartProgram())).start();
}
}