1
1
package jclicker ;
2
2
3
+ import org .jnativehook .GlobalScreen ;
4
+ import org .jnativehook .NativeHookException ;
5
+ import org .jnativehook .keyboard .NativeKeyEvent ;
6
+ import org .jnativehook .keyboard .NativeKeyListener ;
7
+
3
8
import javax .swing .*;
4
9
import java .awt .*;
5
- import java .awt .event .*;
10
+ import java .awt .event .InputEvent ;
11
+ import java .awt .event .MouseAdapter ;
12
+ import java .awt .event .MouseEvent ;
6
13
import java .text .ParseException ;
7
14
import java .util .concurrent .TimeUnit ;
15
+ import java .util .logging .Level ;
16
+ import java .util .logging .Logger ;
8
17
9
18
public class GUI {
10
19
private JComboBox buttonTypeCombo ;
@@ -30,8 +39,44 @@ public class GUI {
30
39
31
40
private Clicker autoClicker = new Clicker ();
32
41
33
- public int hotkeyCode = 118 ;
34
- public String hotkeyText = KeyEvent .getKeyText (hotkeyCode );
42
+ // Set up our hotkey defaults, default key is F7
43
+ public int hotkeyCode = NativeKeyEvent .VC_F7 ;
44
+ public String hotkeyText = NativeKeyEvent .getKeyText (hotkeyCode );
45
+ public boolean waitingForHotkey = false ;
46
+
47
+ // Create a new global hotkey listener
48
+ public class GlobalKeyListener implements NativeKeyListener {
49
+ public void nativeKeyPressed (NativeKeyEvent keyPress ) {
50
+ // If the hotkey is pressed and the clicker isn't running, start it. If it is running, stop it.
51
+ if (keyPress .getKeyCode () == hotkeyCode ) {
52
+ if (setupClicker ()) {
53
+ if (!autoClicker .getRunLoop ()) {
54
+ autoClicker .simulateClicks ();
55
+ } else {
56
+ autoClicker .setRunLoop (false );
57
+ }
58
+ }
59
+ }
60
+
61
+ // If we're waiting for a hotkey to be pressed, make sure we can set it all up
62
+ if (waitingForHotkey ){
63
+ hotkeyCode = keyPress .getKeyCode ();
64
+ hotkeyField .setText (NativeKeyEvent .getKeyText (hotkeyCode ));
65
+
66
+ // Update the buttons when a new hotkey is picked
67
+ startButton .setText ("Start (" + NativeKeyEvent .getKeyText (hotkeyCode ) + ")" );
68
+ stopButton .setText ("Stop (" + NativeKeyEvent .getKeyText (hotkeyCode ) + ")" );
69
+
70
+ waitingForHotkey = false ;
71
+ }
72
+ }
73
+
74
+ public void nativeKeyReleased (NativeKeyEvent e ) {
75
+ }
76
+
77
+ public void nativeKeyTyped (NativeKeyEvent e ) {
78
+ }
79
+ }
35
80
36
81
public static void main (String [] args ) {
37
82
try {
@@ -163,6 +208,27 @@ public GUI() {
163
208
stopButton .setText ("Stop (" + hotkeyText + ")" );
164
209
hotkeyField .setText (hotkeyText );
165
210
211
+ // Create our global hotkey
212
+ try {
213
+ GlobalScreen .registerNativeHook ();
214
+ }catch (NativeHookException e ){
215
+ JOptionPane .showMessageDialog (mainPanel ,
216
+ "Error when setting hotkey" ,
217
+ "Warning" ,
218
+ JOptionPane .WARNING_MESSAGE );
219
+ }
220
+
221
+ // Change our global hotkey's default logging settings
222
+ // Get the logger for "org.jnativehook" and set the level to warning.
223
+ Logger logger = Logger .getLogger (GlobalScreen .class .getPackage ().getName ());
224
+ logger .setLevel (Level .WARNING );
225
+
226
+ // Don't forget to disable the parent handlers.
227
+ logger .setUseParentHandlers (false );
228
+
229
+ // Add the hotkey to the global screen
230
+ GlobalScreen .addNativeKeyListener (new GlobalKeyListener ());
231
+
166
232
167
233
/* // See how many mouse buttons the user's mouse has
168
234
int numMouseButtons = MouseInfo.getNumberOfButtons();
@@ -202,22 +268,8 @@ public void mouseClicked(MouseEvent mouseEvent) {
202
268
// Let the user know that we're waiting on user input
203
269
hotkeyField .setText ("Press Any Key" );
204
270
205
- // Wait for a keypress
206
- setHotkeyButton .addKeyListener (new KeyAdapter () {
207
- @ Override
208
- public void keyPressed (KeyEvent keyEvent ) {
209
- super .keyPressed (keyEvent );
210
-
211
- // Get the keypress code and set that as the keyCode
212
- hotkeyCode = keyEvent .getKeyCode ();
213
- hotkeyField .setText (KeyEvent .getKeyText (hotkeyCode ));
214
-
215
- // Update the buttons when a new hotkey is picked
216
- startButton .setText ("Start (" + KeyEvent .getKeyText (hotkeyCode ) + ")" );
217
- stopButton .setText ("Stop (" + KeyEvent .getKeyText (hotkeyCode ) + ")" );
218
- }
219
- });
220
-
271
+ // We are now waiting for a hotkey
272
+ waitingForHotkey = true ;
221
273
}
222
274
});
223
275
0 commit comments