-
Notifications
You must be signed in to change notification settings - Fork 0
/
Click.java
402 lines (338 loc) · 13.1 KB
/
Click.java
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseMotionListener;
public class Click implements NativeKeyListener,NativeMouseMotionListener{
ArrayList<String> safetyKeys = null;
int mouseButton = 1;
Robot robot = new Robot();
int targetClickCount = 0,
valueOfTag_every = 0,
startDelay = 0,
holdTime = 0,
afterClickDelay=0,
//test comment
terminationDelay = 0;
Point clickLocation = null;//if null, don't move
boolean safetyEnabled = true, press = true,release = true;
boolean hasEveryTag = false;
ArrayList<Point>safetyLocations = null;
String[] defaultSafetyArgs = new String[] {"-safety","0,0,Meta"};
static String supportedKeys = "Backspace\n" +
"Insert\n" + "Home\n" + "Tab\n" + "Q\n" + "W\n" + "E\n" + "R\n" + "T\n" + "Y\n" + "U\n" + "I\n" +
"O\n" + "P\n" + "Delete\n" + "End\n" + "Up\n" + "A\n" + "S\n" + "D\n" + "F\n" + "G\n" + "H\n" + "J\n" + "K\n" + "L\n" +
"Semicolon\n" + "Quote\n" + "Enter\n" + "Clear\n" + "Shift\n" + "Z\n" + "X\n" + "C\n" +
"V\n" + "B\n" + "N\n" + "M\n" + "Comma\n" + "Period\n" + "Slash\n" + "Ctrl\n" + "Meta\n" +
"Alt\n" + "Space\n" + "Left\n" + "Down\n" + "Right\n" + "Space";
public static void main(String[] args) throws InterruptedException, AWTException {
try {
// Get the logger for "org.jnativehook" and set the level to off.
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
// Don't forget to disable the parent handlers.
logger.setUseParentHandlers(false);
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
if(args.length==1 && (args[0].equals("help")|args[0].equals("-help"))) {
System.out.println("Click is command line auto clicker with a built in safety mode to prevent user error causing complete lockups. It is very versatile, with many options to get the desired behavior. \n" +
"\n" +
"OPTIONS\n" +
"*all time is measured in milliseconds.\n" +
"\n" +
"-startDelay TIME = time to wait before starting first click\n" +
"\n" +
"-every TIME = specify time between clicks , makes click run in a loop. The time waited at the end of each click is: ( (-every) - (-hold) ) + (-afterClickDelay)\n" +
"\n" +
"-hold TIME = time to wait before releasing after pressing\n" +
"\n" +
"-afterClickDelay TIME = time to wait after each click. Is not effected by -every or -hold\n" +
"\n" +
"-duration TIME = amount of time to wait after first click before termination \n" +
"\n" +
"-location X,Y = location to move cursor to before each click ( this can make it impossible to move cursor to safety location(s) )\n" +
"\n" +
"-count NUMBER = how many times to click before termination.\n" +
"\n" +
"-button 1-3 = buttons 1,2,3 or l,m,r or left,middle,right\n" +
"\n" +
"-press TRUE | FALSE = press mouse button each time if true, not if false\n" +
"\n" +
"-release TRUE | FALSE = release mouse button each time if true, not if false(script will always release before exiting, unless user kills task outside of click program)\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"-safety = defaults are 0,0,Meta …. defaults are used if -safety tag is not used\n" +
"*Meta is the Windows Key, or Mac Command Key.\n" +
"\n" +
" Examples | Explanations:\n" +
"\n" +
"-safety 0,0,Meta | stop if cursor ever goes to 0,0 or if user presses Meta\n" +
"\n" +
"-safety alt | stop if user presses alt\n" +
"\n" +
"-safety 1920,1080 | stop if cursor ever goes to cords… bottom right of a 1080p screen \n" +
"(might not be reachable depending on OS behavior with edges of screen)\n" +
"\n" +
"-safety alt,ctrl,0,0 | stop if alt is pressed, stop if ctrl is pressed, stop if cursor goes to 0,0. Multiple keys are not shortcuts… they are extra safety keys. Its a safety, not a hotkey!\n" +
"\n" +
"-safety is made to prevent a user from locking up there computer to the point they would have to power it off with the power button. They can meet the safety condition(s) to terminate the process.\n" +
"\n" +
"-safety off or -safety false will turn it off complete. Only do this if you know exactly what you are doing!\n" +
"\n" +
"Option Abbreviations\n" +
"-startDelay -sd\n" +
"-every -e\n" +
"-hold -h\n" +
"-afterClickDelay -ad\n" +
"-duration -d\n" +
"-location -l\n" +
"-count -c\n" +
"-button -b\n" +
"-press -p\n" +
"-release -r\n" +
"-safety -s\n" +
"\n" +
"\n" +
"\n" +
"USAGE:\n" +
"click -every 200 -duration 10000 -button 2\n" +
"(click every 200 mills , terminate after 10 seconds, use mouse button 2/middle click)\n" +
"\n" +
"click -every 200 -count 50 -startDelay 5000 -safety a,s,d,f,0,0\n" +
"(click every 200 mills, terminate after 50 clicks, terminate if user presses any of the specified keys, terminate if cursor moves to 0,0)\n" +
"\n" +
"click -release false -hold 5000\n" +
"(press mouse button once, do not release, hold for 5 seconds, exit, exit always releases mouse button.)\n" +
"\n" +
"\n" +
"*Note\n" +
"On Linux, avoid calling java “-jar path/to/click.jar -every 100”\n" +
"and instead call “click -every 100” with the following command:\n" +
"alias click='java -jar /path/to/click.jar'\n" +
"\n" +
"\n" +
""+ supportedKeys);
System.exit(0);
}else {
Click click = new Click(args);
GlobalScreen.addNativeKeyListener(click);
GlobalScreen.addNativeMouseMotionListener(click);
}
}
public Click(String[] args) throws InterruptedException, AWTException {
//DEBUG ONLY
if(args.length>0) {
//set values from args
setVariablesFromArgs(args);
if(!safetyEnabled) {
System.out.println("Safety is off!");
}else if(safetyEnabled && safetyKeys==null && safetyLocations==null) {
//set default safety
setVariablesFromArgs(defaultSafetyArgs);
System.out.println("Set Default safety to '-safety 0,0,Meta' run 'click -help' for more info.");
}
//if -every tag was not set, prevent from clicking indefinitely
if(hasEveryTag==false && targetClickCount==0) targetClickCount=1;
//CLICK LOOP
new Thread(new Runnable() {
public void run() {
try {
//sleep start delay
if(startDelay>0)System.out.println("Waiting "+ startDelay+"ms ....");
Thread.sleep(startDelay);
//start a thread to terminate process when time runs out
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(terminationDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(terminationDelay!=0)exit(mouseButton,"Exit after -duration of "+terminationDelay+"ms!");
}
}).start();
//start click loop
int buttonMask = MouseEvent.getMaskForButton(mouseButton);
int clicksDone = 0;
long loopStartTime = System.currentTimeMillis();
System.out.println("clicking..." +targetClickCount);
while((clicksDone<targetClickCount || targetClickCount==0) &&
(terminationDelay==0 || (System.currentTimeMillis()-loopStartTime)< terminationDelay )) {
long startTime = System.currentTimeMillis();
if(clickLocation!=null)robot.mouseMove(clickLocation.x, clickLocation.y);
if(press)robot.mousePress(buttonMask);
if(holdTime>0)Thread.sleep(holdTime);
if(release)robot.mouseRelease(buttonMask);
long timeLapsed = System.currentTimeMillis()-startTime;
long timeToSleep = (valueOfTag_every-timeLapsed)+afterClickDelay;
if(timeToSleep>0)Thread.sleep(timeToSleep);
clicksDone++;
}
exit(mouseButton,"Clicking Loop Finished! ClicksDone="+clicksDone);
}catch(InterruptedException e) {
}
}
}).start();
}else {//no arguments, so left click once
new Robot().mousePress(MouseEvent.BUTTON1_MASK);
new Robot().mouseRelease(MouseEvent.BUTTON1_MASK);
exit(mouseButton,"Left Clicked!\nDone!");
}
}
private void setVariablesFromArgs(String[] args) {
try {
int index=0;
for(String arg:args) {//todo handle parsing errors //print out error message to user? lazy way
if(arg.startsWith("-") && index < args.length-1) {
String value = args[index +1];
switch(arg){
//time based int values, all in Milliseconds
case "-every":case "-e":
valueOfTag_every = Integer.parseInt(value);
hasEveryTag=true;
break;
case "-afterClickDelay": case "-ad":
afterClickDelay = Integer.parseInt(value);
break;
case "-duration":case "-d":
terminationDelay = Integer.parseInt(value);
break;
case "-startDelay":case "-sd":
startDelay = Integer.parseInt(value);
break;
case "-hold":case "-h":
holdTime = Integer.parseInt(value);
break;
case "-button":case "-b":
if(value.startsWith("l")) {
mouseButton=1;
}else if(value.startsWith("r")) {
mouseButton=3;
}else if(value.startsWith("m")) {
mouseButton=2;
}else {
mouseButton = Integer.parseInt(value);
}
break;
case "-count": case "-c":
targetClickCount = Integer.parseInt(value);
if(targetClickCount<1 )exit(mouseButton,"Exit: -count value must be above zero!");
break;
case "-location": case "-l":
String[] x_y = value.split(",");
clickLocation = new Point (Integer.parseInt(x_y[0]),Integer.parseInt(x_y[1]));
break;
case "-press": case "-p":
press = Boolean.parseBoolean(value);
break;
case "-release": case"-r":
release = Boolean.parseBoolean(value);
break;
case "-safety":
//examples
// 0,0,escape,alt
// escape,0,0,alt
// escape,alt,0,0
safetyEnabled = !(value.equals("off") || value.equals("false"));
if(safetyEnabled) {
ArrayList<String> supportedKeys = null;
//if safety enabled
String[] safetyStrings = value.split(",");//split by comma
int safetyIndex = 0;
boolean skipIndex = false;
for(String safetyString:safetyStrings) {// escape,0,7
try{
if(!skipIndex) {
int x = Integer.parseInt(safetyString);//fails if not a number
int y = Integer.parseInt(safetyStrings[safetyIndex+1]);
if(safetyLocations==null)safetyLocations=new ArrayList<>();
safetyLocations.add(new Point(x,y));
skipIndex=true;
}else {
skipIndex=false;
//this is definitely a y value, ignore it!
}
}catch(NumberFormatException e) {
//a key
if(safetyKeys==null)safetyKeys=new ArrayList<>();
if(supportedKeys==null)supportedKeys=createSupportedKeysList();
if(supportedKeys.contains(safetyString)) {
safetyKeys.add(safetyString);
}else {exit(mouseButton,"Safety key:"+safetyString +" Not supported, use 'click help' for a list of supported keys.");}
}catch(ArrayIndexOutOfBoundsException e){
System.err.println("Check Your Arguments!a");
System.exit(0);
}
safetyIndex++;
}
}
break;
}
}
index++;
}
}catch(NumberFormatException exception) {
System.err.println("Check Your Arguments!b");
System.exit(0);
}
}
public void exit(int button,String message){
try {
new Robot().mouseRelease(MouseEvent.getMaskForButton(button));
} catch (AWTException e) {
e.printStackTrace();
}
System.out.println(message);
System.exit(0);
}
@Override
public void nativeKeyPressed(NativeKeyEvent arg0) {
int code = arg0.getKeyCode();
String text = NativeKeyEvent.getKeyText(code);
if(safetyKeys!=null) {
if(safetyKeys.contains(text) || safetyKeys.contains(text.toLowerCase()))exit(mouseButton,"Exit Button Pressed!");
}
}
@Override
public void nativeKeyReleased(NativeKeyEvent arg0) {
}
@Override
public void nativeKeyTyped(NativeKeyEvent arg0) {
}
public ArrayList<String> createSupportedKeysList() {
String[] keys = supportedKeys.split("\\n");
ArrayList<String> keysList = new ArrayList<>();
for(String key:keys) {
keysList.add(key);
keysList.add(key.toLowerCase());
}
return keysList;
}
@Override
public void nativeMouseDragged(NativeMouseEvent arg0) {
}
@Override
public void nativeMouseMoved(NativeMouseEvent arg0) {
if(safetyEnabled & safetyLocations!=null) {
Point p = arg0.getPoint();
if(safetyLocations.contains(p)) {
exit(mouseButton,"Exit due to cursor being at a safety location: "+p.x+","+p.y+"!");
}
}
}
}