From f234bafdbe3a575f5ac7f7ad6142ae897cdc34bc Mon Sep 17 00:00:00 2001 From: Xerus <27jf@web.de> Date: Tue, 8 Jan 2019 11:19:14 +0100 Subject: [PATCH] Improve GlobalScreen --- src/java/org/jnativehook/GlobalScreen.java | 54 +++++++++++++--------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/java/org/jnativehook/GlobalScreen.java b/src/java/org/jnativehook/GlobalScreen.java index 5e9cec79..c57e255c 100644 --- a/src/java/org/jnativehook/GlobalScreen.java +++ b/src/java/org/jnativehook/GlobalScreen.java @@ -30,6 +30,7 @@ import java.io.File; import java.util.Iterator; import java.util.concurrent.ExecutorService; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -50,7 +51,13 @@ public class GlobalScreen { /** * Logging service for the native library. */ - protected static Logger log = Logger.getLogger(GlobalScreen.class.getPackage().getName()); + protected static Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName()); + static { + logger.setLevel(Level.WARNING); + logger.setUseParentHandlers(false); + } + + protected GlobalScreen() { } /** * The service to control the hook. @@ -67,7 +74,11 @@ public class GlobalScreen { */ protected static EventListenerList eventListeners = new EventListenerList(); - static { + private static boolean loaded = false; + private static void loadLibrary() { + if(loaded) + return; + loaded = true; String libName = System.getProperty("jnativehook.lib.name", "JNativeHook"); try { @@ -91,7 +102,7 @@ public class GlobalScreen { } catch (Exception e) { // There was a problem instantiating the library loader. - log.severe(e.getMessage()); + logger.severe(e.getMessage()); throw new UnsatisfiedLinkError(e.getMessage()); } @@ -130,13 +141,10 @@ public class GlobalScreen { } } - - protected GlobalScreen() { } - /** * Adds the specified native key listener to receive key events from the - * native system. If listener is null, no exception is thrown and no action - * is performed. + * native system. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native key listener object */ @@ -149,8 +157,8 @@ public static void addNativeKeyListener(NativeKeyListener listener) { /** * Removes the specified native key listener so that it no longer receives * key events from the native system. This method performs no function if - * the listener specified by the argument was not previously added. If - * listener is null, no exception is thrown and no action is performed. + * the listener specified by the argument was not previously added.I + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native key listener object */ @@ -162,8 +170,8 @@ public static void removeNativeKeyListener(NativeKeyListener listener) { /** * Adds the specified native mouse listener to receive mouse events from the - * native system. If listener is null, no exception is thrown and no action - * is performed. + * native system. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native mouse listener object */ @@ -176,8 +184,8 @@ public static void addNativeMouseListener(NativeMouseListener listener) { /** * Removes the specified native mouse listener so that it no longer receives * mouse events from the native system. This method performs no function if - * the listener specified by the argument was not previously added. If - * listener is null, no exception is thrown and no action is performed. + * the listener specified by the argument was not previously added. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native mouse listener object */ @@ -189,8 +197,8 @@ public static void removeNativeMouseListener(NativeMouseListener listener) { /** * Adds the specified native mouse motion listener to receive mouse motion - * events from the native system. If listener is null, no exception is - * thrown and no action is performed. + * events from the native system. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native mouse motion listener object */ @@ -204,8 +212,8 @@ public static void addNativeMouseMotionListener(NativeMouseMotionListener listen * Removes the specified native mouse motion listener so that it no longer * receives mouse motion events from the native system. This method performs * no function if the listener specified by the argument was not previously - * added. If listener is null, no exception is thrown and no action is - * performed. + * added. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native mouse motion listener object */ @@ -217,8 +225,7 @@ public static void removeNativeMouseMotionListener(NativeMouseMotionListener lis /** * Adds the specified native mouse wheel listener to receive mouse wheel - * events from the native system. If listener is null, no exception is - * thrown and no action is performed. + * events from the native system. * * @param listener a native mouse wheel listener object * @since 1.1 @@ -233,8 +240,8 @@ public static void addNativeMouseWheelListener(NativeMouseWheelListener listener * Removes the specified native mouse wheel listener so that it no longer * receives mouse wheel events from the native system. This method performs * no function if the listener specified by the argument was not previously - * added. If listener is null, no exception is thrown and no action is - * performed. + * added. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native mouse wheel listener object * @since 1.1 @@ -408,7 +415,7 @@ public static void registerNativeHook() throws NativeHookException { Thread.sleep(250); } catch (InterruptedException e) { - log.warning(e.getMessage()); + logger.warning(e.getMessage()); break; } } @@ -420,6 +427,7 @@ public static void registerNativeHook() throws NativeHookException { hookThread = new NativeHookThread(); synchronized (hookThread) { + loadLibrary(); hookThread.start(); try {