Skip to content
This repository was archived by the owner on Jul 10, 2020. It is now read-only.

Commit 3830d8b

Browse files
committed
Added support for global hotkey to start/stop the clicker. Introducted a third-part library. Reworked the hotkey setting function.
1 parent 12d2a0a commit 3830d8b

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

.idea/libraries/jnativehook_2_1_0.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JClicker.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="jnativehook-2.1.0" level="project" />
1011
</component>
1112
</module>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ JClicker is an extremely lightweight autoclicker made in Java with an emphasis o
1212
- Stop the clicker only when stopped
1313
- Change hotkey to start and stop the clicker
1414
- Validation checks on inputted values
15+
- Global hotkey to start/stop the clicker (some keys are not supported)
1516

1617
## Screenshot
1718
![](https://i.imgur.com/p0Y5V6T.png)
@@ -20,7 +21,6 @@ JClicker is an extremely lightweight autoclicker made in Java with an emphasis o
2021
[![GitHub (pre-)release](https://img.shields.io/github/release/Bonfire/JClicker/all.svg)](https://github.com/Bonfire/JClicker/releases)
2122

2223
## Planned Features
23-
- Implement a global hotkey (window doesn't have to be focused)
2424
- Detect and allow the setting of different mouse buttons (mouse-4, mouse-5, etc.)
2525

2626
## Issues

src/jclicker/GUI.java

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package jclicker;
22

3+
import org.jnativehook.GlobalScreen;
4+
import org.jnativehook.NativeHookException;
5+
import org.jnativehook.keyboard.NativeKeyEvent;
6+
import org.jnativehook.keyboard.NativeKeyListener;
7+
38
import javax.swing.*;
49
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;
613
import java.text.ParseException;
714
import java.util.concurrent.TimeUnit;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
817

918
public class GUI {
1019
private JComboBox buttonTypeCombo;
@@ -30,8 +39,44 @@ public class GUI {
3039

3140
private Clicker autoClicker = new Clicker();
3241

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+
}
3580

3681
public static void main(String[] args) {
3782
try {
@@ -163,6 +208,27 @@ public GUI() {
163208
stopButton.setText("Stop (" + hotkeyText + ")");
164209
hotkeyField.setText(hotkeyText);
165210

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+
166232

167233
/* // See how many mouse buttons the user's mouse has
168234
int numMouseButtons = MouseInfo.getNumberOfButtons();
@@ -202,22 +268,8 @@ public void mouseClicked(MouseEvent mouseEvent) {
202268
// Let the user know that we're waiting on user input
203269
hotkeyField.setText("Press Any Key");
204270

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;
221273
}
222274
});
223275

0 commit comments

Comments
 (0)