From 69c345b6de12368a69dd5f0cac31e1d97cdbbc71 Mon Sep 17 00:00:00 2001 From: Xerus <27jf@web.de> Date: Tue, 19 Mar 2019 16:55:11 +0100 Subject: [PATCH] Improve GlobalScreen --- src/java/org/jnativehook/GlobalScreen.java | 84 +++++++++++----------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/src/java/org/jnativehook/GlobalScreen.java b/src/java/org/jnativehook/GlobalScreen.java index 5e9cec79..7e44b3c0 100644 --- a/src/java/org/jnativehook/GlobalScreen.java +++ b/src/java/org/jnativehook/GlobalScreen.java @@ -1,5 +1,5 @@ /* JNativeHook: Global keyboard and mouse hooking for Java. - * Copyright (C) 2006-2018 Alexander Barker. All Rights Received. + * Copyright (C) 2006-2018 Alexander Barker. All Rights Received. * https://github.com/kwhat/jnativehook/ * * JNativeHook is free software: you can redistribute it and/or modify @@ -9,11 +9,11 @@ * * JNativeHook 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 + * 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 Lesser General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ package org.jnativehook; @@ -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. @@ -91,7 +98,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 +137,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 +153,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 +166,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 +180,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 +193,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 +208,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 +221,8 @@ 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. + * If listener is null, no exception is thrown and no action is performed. * * @param listener a native mouse wheel listener object * @since 1.1 @@ -233,8 +237,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 @@ -346,8 +350,8 @@ public NativeHookException getException() { } /** - * Native implementation to start the input hook. This method blocks and should only be called by this - * specialized thread implementation. This method will notifyAll() after passing any exception exception + * Native implementation to start the input hook. This method blocks and should only be called by this + * specialized thread implementation. This method will notifyAll() after passing any exception exception * throwing code. * * @throws NativeHookException problem registering the native hook with the underlying operating system. @@ -355,16 +359,16 @@ public NativeHookException getException() { protected native void enable() throws NativeHookException; /** - * Native implementation to stop the input hook. There is no other way to stop the hook. + * Native implementation to stop the input hook. There is no other way to stop the hook. * * @throws NativeHookException problem un-registering the native hook with the underlying operating system. */ public native void disable() throws NativeHookException; /** - * Dispatches an event to the appropriate processor. This method is + * Dispatches an event to the appropriate processor. This method is * generally called by the native library but may be used to synthesize - * native events from Java without replaying them on the native system. If + * native events from Java without replaying them on the native system. If * you would like to send events to other applications, please use * {@link #postNativeEvent}, *

@@ -408,7 +412,7 @@ public static void registerNativeHook() throws NativeHookException { Thread.sleep(250); } catch (InterruptedException e) { - log.warning(e.getMessage()); + logger.warning(e.getMessage()); break; } } @@ -480,36 +484,36 @@ public static boolean isNativeHookRegistered() { * * For both NATIVE_KEY_PRESSED and * NATIVE_KEY_RELEASED events, the virtual keycode and modifier - * mask are used in the creation of the native event. Please note that some + * mask are used in the creation of the native event. Please note that some * platforms may generate NATIVE_KEY_PRESSED and * NATIVE_KEY_RELEASED events for each required modifier. * NATIVE_KEY_TYPED events will first translate the associated * keyChar to its respective virtual code and then produce a * NATIVE_KEY_PRESSED followed by a NATIVE_KEY_RELEASED - * event using that virtual code. If the JNativeHook is unable to translate + * event using that virtual code. If the JNativeHook is unable to translate * the keyChar to its respective virtual code, the event is ignored. *

* * NativeMouseEvents are processed in much the same way as the - * NativeKeyEvents. Both NATIVE_MOUSE_PRESSED and + * NativeKeyEvents. Both NATIVE_MOUSE_PRESSED and * NATIVE_MOUSE_RELEASED produce events corresponding to the - * event's button code. Keyboard modifiers may be used in conjunction with + * event's button code. Keyboard modifiers may be used in conjunction with * button press and release events, however, they might produce events for each - * modifier. NATIVE_MOUSE_CLICKED events produce a + * modifier. NATIVE_MOUSE_CLICKED events produce a * NATIVE_MOUSE_PRESSED event followed by a * NATIVE_MOUSE_RELEASED for the assigned event button. *

* * NATIVE_MOUSE_DRAGGED and NATIVE_MOUSE_MOVED events - * are handled identically. In order to produce a NATIVE_MOUSE_DRAGGED + * are handled identically. In order to produce a NATIVE_MOUSE_DRAGGED * event, you must specify a button modifier mask that contains at least one - * button modifier and assign it to the event. Failure to do so will produce a + * button modifier and assign it to the event. Failure to do so will produce a * NATIVE_MOUSE_MOVED event even if the event id was set to * NATIVE_MOUSE_DRAGGED. *

* * NATIVE_MOUSE_WHEEL events are identical to - * NATIVE_MOUSE_PRESSED events. Wheel events will only produce + * NATIVE_MOUSE_PRESSED events. Wheel events will only produce * pressed events and will never produce NATIVE_MOUSE_RELEASED, * NATIVE_MOUSE_DRAGGED or NATIVE_MOUSE_MOVED * @@ -666,9 +670,9 @@ private void processMouseWheelEvent(NativeMouseWheelEvent nativeEvent) { } /** - * Set a different executor service for native event delivery. By default, + * Set a different executor service for native event delivery. By default, * JNativeHook utilizes a single thread executor to dispatch events from - * the native event queue. You may choose to use an alternative approach + * the native event queue. You may choose to use an alternative approach * for event delivery by implementing an ExecutorService. *

* Note: Using null as an ExecutorService will cause all