register the native hook only once
This commit is contained in:
@@ -15,6 +15,9 @@ public class Main {
|
|||||||
private static final Logger LOGGER = LogManager.getLogger(Main.class);
|
private static final Logger LOGGER = LogManager.getLogger(Main.class);
|
||||||
private static final MainWindow mainWindow = new MainWindow();
|
private static final MainWindow mainWindow = new MainWindow();
|
||||||
|
|
||||||
|
/** Whether the hook and its listener are registered, so retries don't add duplicates. */
|
||||||
|
private static boolean hookRegistered = false;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
LOGGER.info("Hello world!");
|
LOGGER.info("Hello world!");
|
||||||
|
|
||||||
@@ -39,10 +42,15 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean registerJNativeHook() {
|
public static boolean registerJNativeHook() {
|
||||||
|
if (hookRegistered) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
LOGGER.info("Registering jnativehook library...");
|
LOGGER.info("Registering jnativehook library...");
|
||||||
try {
|
try {
|
||||||
GlobalScreen.registerNativeHook();
|
GlobalScreen.registerNativeHook();
|
||||||
GlobalScreen.addNativeKeyListener(new KeyPressListener());
|
GlobalScreen.addNativeKeyListener(new KeyPressListener());
|
||||||
|
hookRegistered = true;
|
||||||
LOGGER.info("Successfully registered jnativehook library!");
|
LOGGER.info("Successfully registered jnativehook library!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user