Skip to content

Commit

Permalink
Improve GlobalScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerus committed Jan 8, 2019
1 parent e8337e2 commit f234baf
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/java/org/jnativehook/GlobalScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -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());
}
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -420,6 +427,7 @@ public static void registerNativeHook() throws NativeHookException {
hookThread = new NativeHookThread();

synchronized (hookThread) {
loadLibrary();
hookThread.start();

try {
Expand Down

0 comments on commit f234baf

Please sign in to comment.