/
EventNazi.java
744 lines (689 loc) · 30.6 KB
/
EventNazi.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
* Copyright 2006-2013 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of the SMART Minimizing Autoing Resource Thing (SMART)
*
* SMART is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SMART is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SMART. If not, see <http://www.gnu.org/licenses/>.
*/
package smart;
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
/**
* The EventNazi is responsible for creating events to send to the client. It
* automaticaly handles focus just as a normal client would, by loosing when you
* click off, and getting when you click on. It abstracts key control into two
* methods, one to send a string, and one to hold a key. It abstracts mouse
* control into three, one for moving, one for dragging, and one for clicking.
*
* This class also contains the newest in my line of mouse algorithms, WindMouse.
* As the name would suggest, the mouse is givin randomness as if a smooth but
* shifting wind were pushing it around. All the while, it is being "pulled"
* towards the destination as if by gravity. Supprisingly this is a very good
* emuation of human mouse movement.
*
* The comment "Scratch" denotes pieces of the mouse debug output code that
* should be commented out when not using.
*
* NOTE: keycodes are the windows Virtual-Key Scan Codes
*
* @author Benjamin J. Land
*/
public class EventNazi {
/**
* Contains all created and active EventNazi instances.
*/
private static final HashMap<Component,EventNazi> nazies = new HashMap<Component,EventNazi>();
/**
* Returns the EventNazi for the specified Component, if it exists. Otherwise
* it creates the Nazi and sets it up. Only the BlockingEventQueue should
* use this method.
* @param comp The component to create a nazi for.
* @result The new/old EventNazi
*/
public static EventNazi createNazi(Component comp) {
EventNazi nazi = nazies.containsKey(comp) ? nazies.get(comp) : new EventNazi(comp);
if (!nazi.active) {
if (comp.getMousePosition() != null && comp.isFocusOwner() && comp.isShowing()) {
nazi.cx = comp.getMousePosition().x;
nazi.cy = comp.getMousePosition().y;
nazi.mousein = true;
} else {
nazi.cx = -1;
nazi.cy = -1;
nazi.mousein = false;
}
nazi.active = BlockingEventQueue.isBlocking(comp);
nazi.focused = comp.isFocusOwner() && comp.isShowing();
}
nazies.put(comp, nazi);
return nazi;
}
/**
* Returns the EventNazi for the specified Component, if it exists. Null
* otherwise. Other classes should use this method.
* @param comp The comonent to check for.
* @result See description
*/
public static EventNazi getNazi(Component comp) {
return nazies.get(comp);
}
private Component comp;
private KeySender keySender;
private boolean active;
private int cx, cy;
private Set<int[]> keysHeld;
private boolean mousein;
private boolean leftDown;
private boolean midDown;
private boolean rightDown;
private boolean focused;
private boolean shiftDown;
//Scratch
/*private JFrame frame = new JFrame ("WindMouse Debug");
private BufferedImage scratch = new BufferedImage(765,503,BufferedImage.TYPE_INT_ARGB);
private Graphics g = scratch.getGraphics();*/
/**
* Creates a new instance of EventNazi initialized to a "null" but ready
* state. Private because EventNazi need to be created by the createNazi
* method.
*/
private EventNazi(Component comp) {
//Scratch
/*frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridLayout());
frame.add(new JLabel(new ImageIcon(scratch)));
frame.pack();
frame.setVisible(true);
g.setColor(Color.BLACK);*/
nazies.put(comp, this);
this.comp = comp;
active = false;
leftDown = false;
rightDown = false;
focused = false;
shiftDown = false;
keysHeld = Collections.synchronizedSet(new HashSet<int[]>());
keySender = new KeySender("java-key-thread");
keySender.start();
}
/**
* Returns the component that this EventNazi is controlling.
* @result This EventNazi's component.
*/
public Component getComponent() {
return comp;
}
public void wait(int min, int max) {
int time = min == max ? min : (int)((Math.random() * Math.abs(max - min)) + Math.min(min, max));
try { Thread.sleep(time); } catch (Exception e) { e.printStackTrace(); }
}
public void wait(int mills) {
wait(mills, mills);
}
public Point getMousePos() {
return new Point(cx,cy);
}
private synchronized Point moveMouseImpl(int x, int y) {
int btnMask = (leftDown ? MouseEvent.BUTTON1_DOWN_MASK : 0) | (rightDown ? (MouseEvent.BUTTON3_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0);
if (isDragging()) {
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_DRAGGED,System.currentTimeMillis(),btnMask,x,y,0,false,0));
} else if (x >= 0 && x < comp.getWidth() && y >= 0 && y < comp.getHeight()) {
if (mousein) {
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_MOVED,System.currentTimeMillis(),btnMask,x,y,0,false,0));
} else {
mousein = true;
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_ENTERED,System.currentTimeMillis(),btnMask,x,y,0,false,0));
}
} else {
if (mousein) {
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_EXITED,System.currentTimeMillis(),btnMask,x,y,0,false,0));
mousein = false;
} else {
//Mouse outside and still out, no event.
}
}
cx = x;
cy = y;
return new Point(cx, cy);
}
/**
* Internal mouse movement algorithm. Do not use this without credit to either
* Benjamin J. Land or BenLand100. This is synchronized to prevent multiple
* motions and bannage.
* @param xs The x start
* @param ys The y start
* @param xe The x destination
* @param ye The y destination
* @param gravity Strength pulling the position towards the destination
* @param wind Strength pulling the position in random directions
* @param minWait Minimum relative time per step
* @param maxWait Maximum relative time per step
* @param maxStep Maximum size of a step, prevents out of control motion
* @param targetArea Radius of area around the destination that should
* trigger slowing, prevents spiraling
* @result The actual end point
*/
private synchronized Point windMouseImpl(double xs, double ys, double xe, double ye, double gravity, double wind, double minWait, double maxWait, double maxStep, double targetArea) {
//System.out.println(targetArea);
final double sqrt3 = Math.sqrt(3);
final double sqrt5 = Math.sqrt(5);
double dist, veloX = 0, veloY = 0, windX = 0, windY = 0;
while ((dist = Math.hypot(xs - xe,ys - ye)) >= 1) {
wind = Math.min(wind, dist);
if (dist >= targetArea) {
windX = windX / sqrt3 + (Math.random() * (wind * 2D + 1D) - wind) / sqrt5;
windY = windY / sqrt3 + (Math.random() * (wind * 2D + 1D) - wind) / sqrt5;
} else {
windX /= sqrt3;
windY /= sqrt3;
if (maxStep < 3) {
maxStep = Math.random() * 3 + 3D;
} else {
maxStep /= sqrt5;
}
//System.out.println(maxStep + ":" + windX + ";" + windY);
}
veloX += windX + gravity * (xe - xs) / dist;
veloY += windY + gravity * (ye - ys) / dist;
double veloMag = Math.hypot(veloX, veloY);
if (veloMag > maxStep) {
double randomDist = maxStep / 2D + Math.random() * maxStep / 2D;
veloX = (veloX / veloMag) * randomDist;
veloY = (veloY / veloMag) * randomDist;
}
xs += veloX;
ys += veloY;
int mx = (int) Math.round(xs);
int my = (int) Math.round(ys);
if (cx != mx || cy != my) {
//Scratch
/*g.drawLine(cx,cy,mx,my);
frame.repaint();*/
//MouseJacking
/*try {
Robot r = new Robot();
r.mouseMove(mx,my);
} catch (Exception e) { } */
moveMouseImpl(mx, my);
}
double step = Math.hypot(xs - cx, ys - cy);
try {
Thread.sleep(Math.round((maxWait - minWait) * (step / maxStep) + minWait));
} catch (InterruptedException ex) { }
}
//System.out.println(Math.abs(xe - cx) + ", " + Math.abs(ye - cy));
return new Point(cx, cy);
}
/**
* Moves the mouse from the current position to the specified position.
* Approximates human movement in a way where smoothness and accuracy are
* relative to speed, as it should be.
* @param x The x destination
* @param y The y destination
* @result The actual end point
*/
public synchronized Point windMouse(int x, int y) {
if (canInteract()) {
double speed = (Math.random() * 15D + 15D) / 10D;
return windMouseImpl(cx,cy,x,y,9D,3D,5D/speed,10D/speed,10D*speed,8D*speed);
}
return null;
}
/**
* Moves the mouse from the current position to the specified position.
* Approximates human movement in a way where smoothness and accuracy are
* relative to speed, as it should be.
* @param x The x destination
* @param y The y destination
* @result The actual end point
*/
public synchronized Point moveMouse(int x, int y) {
if (canInteract()) {
return moveMouseImpl(x,y);
}
return null;
}
/**
* Holds the mouse at the specified position after moving from the current
* position to the specified position.
* @param x The x destination
* @param y The y destination
* @result The actual end point
*/
public synchronized Point holdMouse(int x, int y, int button) {
if (canHold(button)) {
int btnMask = ((leftDown || button==1) ? MouseEvent.BUTTON1_DOWN_MASK : 0) | ((midDown || button==2) ? (MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0) | ((rightDown || button==3) ? (MouseEvent.BUTTON3_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0);
int btn = 0;
switch (button) {
case 1: btn = MouseEvent.BUTTON1; break;
case 2: btn = MouseEvent.BUTTON2; break;
case 3: btn = MouseEvent.BUTTON3; break;
}
Point end = moveMouse(x,y);
if (mousein) {
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_PRESSED,System.currentTimeMillis(),btnMask,cx,cy,1,false,btn));
if (!focused) {
wait(25,50);
getFocus();
}
switch (button) {
case 1: leftDown = true; break;
case 2: midDown = true; break;
case 3: rightDown = true; break;
}
}
return end;
}
return null;
}
/**
* Releases the mouse at the specified position after moving from the current
* position to the specified position.
* @param x The x destination
* @param y The y destination
* @result The actual end point
*/
public synchronized Point releaseMouse(int x, int y, int button) {
if (canRelease(button)) {
int btnMask = ((leftDown || button==1) ? MouseEvent.BUTTON1_DOWN_MASK : 0) | ((midDown || button==2) ? (MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0) | ((rightDown || button==3) ? (MouseEvent.BUTTON3_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0);
int btn = 0;
switch (button) {
case 1: btn = MouseEvent.BUTTON1; break;
case 2: btn = MouseEvent.BUTTON2; break;
case 3: btn = MouseEvent.BUTTON3; break;
}
Point end = moveMouse(x,y);
if (mousein) {
long time = System.currentTimeMillis();
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_RELEASED,time,btnMask,end.x,end.y,1,false,btn));
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_CLICKED,time,btnMask,end.x,end.y,1,false,btn));
switch (button) {
case 1: leftDown = false; break;
case 2: midDown = false; break;
case 3: rightDown = false; break;
}
} else {
loseFocus(false);
}
return end;
}
return null;
}
/**
* Clicks the mouse at the specified position after moving from the current
* position to the specified position.
* @param x The x destination
* @param y The y destination
* @result The actual end point
*/
public synchronized Point clickMouse(int x, int y, int button) {
if (canClick(button)) {
int btnMask = ((leftDown || button==1) ? MouseEvent.BUTTON1_DOWN_MASK : 0) | ((midDown || button==2) ? (MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0) | ((rightDown || button==3) ? (MouseEvent.BUTTON3_DOWN_MASK | MouseEvent.META_DOWN_MASK) : 0);
int btn = 0;
switch (button) {
case 1: btn = MouseEvent.BUTTON1; break;
case 2: btn = MouseEvent.BUTTON2; break;
case 3: btn = MouseEvent.BUTTON3; break;
}
Point end = moveMouse(x,y);
if (mousein) {
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_PRESSED,System.currentTimeMillis(),btnMask,cx,cy,1,false,btn));
switch (button) {
case 1: leftDown = true; break;
case 2: midDown = true; break;
case 3: rightDown = true; break;
}
if (!focused) {
wait(25,50);
getFocus();
}
try { Thread.sleep((int)(Math.random() * 56 + 90)); } catch (Exception ex) { }
long time = System.currentTimeMillis();
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_RELEASED,time,btnMask,end.x,end.y,1,false,btn));
BlockingEventQueue.sendUnblocked(new MouseEvent(comp,MouseEvent.MOUSE_CLICKED,time,btnMask,end.x,end.y,1,false,btn));
switch (button) {
case 1: leftDown = false; break;
case 2: midDown = false; break;
case 3: rightDown = false; break;
}
} else {
loseFocus(false);
}
return end;
}
return null;
}
public synchronized boolean isMouseButtonHeld(int button) {
switch (button) {
case 1: return leftDown;
case 2: return midDown;
case 3: return rightDown;
}
return false;
}
public synchronized Point scrollMouse(int x, int y, int lines) {
int btnMask = (isKeyDown(KeyEvent.VK_SHIFT) ? KeyEvent.SHIFT_MASK : 0) | (isKeyDown(KeyEvent.VK_ALT) ? KeyEvent.ALT_MASK : 0) | (isKeyDown(KeyEvent.VK_CONTROL) ? KeyEvent.CTRL_MASK : 0);
if (canInteract()) {
Point end = moveMouse(x,y);
if (mousein) {
BlockingEventQueue.sendUnblocked(new MouseWheelEvent(comp, MouseEvent.MOUSE_WHEEL, System.currentTimeMillis(), btnMask, x, y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, Math.abs(lines), lines < 0 ? -1 : 1));
return new Point(x, y);
}
return end;
}
return null;
}
/**
* Tests if a character requires the shift key to be pressed.
* @param c Char to check for
* @result True if shift is required
*/
private boolean isShiftChar(char c) {
String special = "~!@#$%^&*()_+|{}:\"<>?";
return special.indexOf(c) != -1 || (c - 'A' >= 0 && c - 'A' <= 25);
}
public static int[] typable_vk_keycode = new int[0xff];
static {
typable_vk_keycode[32] = 32;
for (int c = (int)'A'; c <= (int)'Z'; c++) typable_vk_keycode[c] = c;
for (int c = (int)'0'; c <= (int)'9'; c++) typable_vk_keycode[c] = c;
typable_vk_keycode[186] = ';'; // ;:
typable_vk_keycode[187] = '='; // =+
typable_vk_keycode[188] = ','; // hack: ,
typable_vk_keycode[189] = '-'; // -_
typable_vk_keycode[190] = '.'; // .>
typable_vk_keycode[191] = '/'; // /?
typable_vk_keycode[192] = '`'; // `~
typable_vk_keycode[219] = '['; // [{
typable_vk_keycode[220] = '\\';// \|
typable_vk_keycode[221] = ']'; // ]}
typable_vk_keycode[222] = '\'';// '"
typable_vk_keycode[226] = ','; // hack: <
}
/**
* Converts a char into a KeyCode value for KeyEvent
* @param c Char to convert
* @result c's KeyCode
*/
private int toKeyCode(char c) {
final String special = "~!@#$%^&*()_+|{}:\"<>?";
final String normal = "`1234567890-=\\[];',./";
int index = special.indexOf(c);
return Character.toUpperCase(index == -1 ? c : normal.charAt(index));
}
/**
* Converts a vk code into a char
* @param code KeyCode to convert
* @result the char
*/
private char toChar(int vk, boolean shift) {
int code = typable_vk_keycode[vk];
final String special = "~!@#$%^&*()_+|{}:\"<>?";
final String normal = "`1234567890-=\\[];',./";
int index = normal.indexOf((char)code);
if (index == -1) {
return shift ? Character.toUpperCase((char)code) : Character.toLowerCase((char)code);
} else {
return shift ? special.charAt(index) : (char)code;
}
}
/**
* Returns true if the vk code is typable
*/
private boolean isTypableCode(int vk) {
return vk < 0xff && typable_vk_keycode[vk] != 0;
}
/**
* Holds a key. Should be used for any key that needs to be held, not
* sending text.
* @param code KeyCode for the key
*/
public synchronized void holdKey(int code) {
if (canInteract()) {
if (!focused) getFocus();
long startTime = System.currentTimeMillis();
int[] dat = new int[] {code, (int) (startTime & 0xFFFFFFFF)};
if (!isKeyHeld(dat)) {
if (KeyEvent.VK_SHIFT == code) shiftDown = true;
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED, startTime, shiftDown ? KeyEvent.SHIFT_DOWN_MASK : 0, code, KeyEvent.CHAR_UNDEFINED, KeyEvent.KEY_LOCATION_STANDARD));
if (isTypableCode(code)) {
//System.out.println("Trying to type " + code + " as '" + toChar(code,shiftDown) + "'");
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_TYPED, startTime, shiftDown ? KeyEvent.SHIFT_DOWN_MASK : 0, 0, toChar(code,shiftDown), KeyEvent.KEY_LOCATION_UNKNOWN));
}
setKeyHeld(dat, true);
}
}
}
/**
* Release a key. Should be used for any key that needs to be held, not
* sending text. Will only release it if its already held.
* @param code KeyCode for the key
*/
public synchronized void releaseKey(int code) {
if (canInteract()) {
if (!focused) getFocus();
long startTime = System.currentTimeMillis();
int[] dat = new int[] {code};
if (isKeyHeld(dat)) {
setKeyHeld(dat, false);
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_RELEASED, startTime, 0, code, KeyEvent.CHAR_UNDEFINED, KeyEvent.KEY_LOCATION_STANDARD));
if (KeyEvent.VK_SHIFT == code) shiftDown = false;
}
}
}
/**
* Allows KeyEvents to be easily and safely passed through the blocking
* mechanism. Also ensures the target component is the target of this nazi
*/
public synchronized void passKeyEvent(KeyEvent e) {
if (canInteract()) {
if (!focused) getFocus();
//System.out.println("Passing Event: " + e.getKeyChar());
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation()));
}
}
public boolean isKeyDown(int code) {
int[] dat = new int[] {code};
return isKeyHeld(dat);
}
/**
* Sends a string to the client like a person would type it.
* In Scar you can use Chr(10) for enter, not Chr(13)
* Not to be used for arrow keys, but can be used with F keys or the like
* @param text String to send to the client
*/
public synchronized void sendKeys(String text, int keywait, int keymodwait) {
if (canInteract()) {
char[] chars = text.toCharArray();
if (!focused) getFocus();
for (char c : chars) {
int code = toKeyCode(c);
int keyLoc = Character.isDigit(c) ? Math.random() > 0.5D ? KeyEvent.KEY_LOCATION_NUMPAD : KeyEvent.KEY_LOCATION_STANDARD : KeyEvent.KEY_LOCATION_STANDARD;
if (isShiftChar(c)) {
int shiftLoc = Math.random() > 0.5D ? KeyEvent.KEY_LOCATION_RIGHT : KeyEvent.KEY_LOCATION_LEFT;
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_MASK, KeyEvent.VK_SHIFT, KeyEvent.CHAR_UNDEFINED, shiftLoc));
try { Thread.sleep((int)((Math.random() * 0.1 + 1) * keymodwait)); } catch (Exception e) { e.printStackTrace(); }
long time = System.currentTimeMillis();
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED, time, KeyEvent.SHIFT_MASK, code, c, keyLoc));
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_TYPED, time, KeyEvent.SHIFT_MASK, 0, c, KeyEvent.KEY_LOCATION_UNKNOWN));
try { Thread.sleep((int)((Math.random() * 0.1 + 1) * keywait)); } catch (Exception e) { e.printStackTrace(); }
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), KeyEvent.SHIFT_MASK, code, c, keyLoc));
try { Thread.sleep((int)((Math.random() * 0.1 + 1) * keymodwait)); } catch (Exception e) { e.printStackTrace(); }
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_SHIFT, KeyEvent.CHAR_UNDEFINED, shiftLoc));
} else {
long time = System.currentTimeMillis();
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED, time, 0, code, c, keyLoc));
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_TYPED, time, 0, 0, c, KeyEvent.KEY_LOCATION_UNKNOWN));
try { Thread.sleep((int)((Math.random() * 0.1 + 1) * keywait)); } catch (Exception e) { e.printStackTrace(); }
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, code, c, keyLoc));
}
}
}
}
/**
* Sends an event that emulates a user alt+tabbing into RuneScape.
*/
private synchronized void getFocus() {
if (!focused) {
BlockingEventQueue.sendUnblocked(new FocusEvent(comp, FocusEvent.FOCUS_GAINED, false, null));
focused = true;
wait(100,200);
}
}
/**
* Sends an event that emulates a user alt+tabbing out of RuneScape.
* tabbed = true will send a tab key, so avoid that.
*/
private synchronized void loseFocus(boolean tabbed) {
if (focused) {
if (tabbed) {
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED,System.currentTimeMillis(),KeyEvent.ALT_DOWN_MASK,KeyEvent.VK_ALT,KeyEvent.CHAR_UNDEFINED,KeyEvent.KEY_LOCATION_LEFT));
wait(100,200);
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED,System.currentTimeMillis(),KeyEvent.ALT_DOWN_MASK,KeyEvent.VK_TAB,KeyEvent.CHAR_UNDEFINED,KeyEvent.KEY_LOCATION_STANDARD));
wait(10,50);
}
BlockingEventQueue.sendUnblocked(new FocusEvent(comp, FocusEvent.FOCUS_LOST, false, null));
BlockingEventQueue.sendUnblocked(new FocusEvent(comp, FocusEvent.FOCUS_LOST, false, null));
focused = false;
wait(100,200);
}
}
private synchronized void setKeyHeld(int[] dat, boolean held) {
synchronized (keysHeld) {
if (held) {
keysHeld.add(dat);
} else {
HashSet<int[]> remove = new HashSet<int[]>();
for (int[] entry : keysHeld) {
if (entry[0] == dat[0]) {
remove.add(entry);
}
}
keysHeld.removeAll(remove);
}
}
}
private synchronized boolean isKeyHeld(int[] dat) {
synchronized (keysHeld) {
for (int[] entry : keysHeld) {
if (entry[0] == dat[0]) {
return true;
}
}
return false;
}
}
/**
* Kills this EvetNazi without making bad events possible.
*/
public synchronized void destroy() {
//Scratch
/*frame.dispose();*/
keySender.stop();
synchronized (keysHeld) {
for (int[] dat : keysHeld) {
releaseKey(dat[1]);
}
}
Point pos;
if ((pos = comp.getMousePosition()) != null && !mousein) {
moveMouse(pos.x,pos.y);
} else if (focused && pos == null && mousein) {
moveMouse(-10,-10);
}
if (focused) {
//how to handle this? probably safe to just pass the lose focus event
//anything else is INCREDIBLY annoying since RS destroys the canvas so often
loseFocus(false);
}
nazies.remove(comp);
comp = null;
active = false;
}
public synchronized boolean isActive() {
if (!BlockingEventQueue.isBlocking(comp)) destroy();
return active;
}
public synchronized boolean isFocused() {
return focused;
}
public synchronized boolean isDragging() {
return leftDown || midDown || rightDown;
}
public synchronized boolean isDown(int button) {
switch (button) {
case 1: return leftDown;
case 2: return midDown;
case 3: return rightDown;
}
return false;
}
public synchronized boolean canInteract() {
return active;
}
public synchronized boolean canClick(int button) {
switch (button) {
case 1: return canInteract() && !leftDown;
case 2: return canInteract() && !midDown;
case 3: return canInteract() && !rightDown;
}
return false;
}
public synchronized boolean canHold(int button) {
switch (button) {
case 1: return canInteract() && !leftDown;
case 2: return canInteract() && !midDown;
case 3: return canInteract() && !rightDown;
}
return false;
}
public synchronized boolean canRelease(int button) {
switch (button) {
case 1: return canInteract() && leftDown;
case 2: return canInteract() && midDown;
case 3: return canInteract() && rightDown;
}
return false;
}
private class KeySender extends Thread {
public KeySender(String name) {
super(name);
}
public void run() {
while (true) {
try { Thread.sleep(30 + (int)(Math.random()*5)); } catch (InterruptedException ex) { }
int minTime = (int) (System.currentTimeMillis() & 0xFFFFFFFF) - 1000;
synchronized (keysHeld) {
for (int[] dat : keysHeld) {
if (dat[1] < minTime) {
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), shiftDown ? KeyEvent.SHIFT_DOWN_MASK : 0, dat[0], KeyEvent.CHAR_UNDEFINED, KeyEvent.KEY_LOCATION_STANDARD));
if (isTypableCode(dat[0]))
BlockingEventQueue.sendUnblocked(new KeyEvent(comp, KeyEvent.KEY_TYPED, System.currentTimeMillis(), shiftDown ? KeyEvent.SHIFT_DOWN_MASK : 0, 0, toChar(dat[0],shiftDown), KeyEvent.KEY_LOCATION_UNKNOWN));
}
}
}
yield();
}
}
}
}