3030import java .io .File ;
3131import java .util .Iterator ;
3232import java .util .concurrent .ExecutorService ;
33+ import java .util .logging .Level ;
3334import java .util .logging .Logger ;
3435
3536/**
@@ -50,7 +51,13 @@ public class GlobalScreen {
5051 /**
5152 * Logging service for the native library.
5253 */
53- protected static Logger log = Logger .getLogger (GlobalScreen .class .getPackage ().getName ());
54+ protected static Logger logger = Logger .getLogger (GlobalScreen .class .getPackage ().getName ());
55+ static {
56+ logger .setLevel (Level .WARNING );
57+ logger .setUseParentHandlers (false );
58+ }
59+
60+ protected GlobalScreen () { }
5461
5562 /**
5663 * The service to control the hook.
@@ -67,7 +74,11 @@ public class GlobalScreen {
6774 */
6875 protected static EventListenerList eventListeners = new EventListenerList ();
6976
70- static {
77+ private static boolean loaded = false ;
78+ private static void loadLibrary () {
79+ if (loaded )
80+ return ;
81+ loaded = true ;
7182 String libName = System .getProperty ("jnativehook.lib.name" , "JNativeHook" );
7283
7384 try {
@@ -91,7 +102,7 @@ public class GlobalScreen {
91102 }
92103 catch (Exception e ) {
93104 // There was a problem instantiating the library loader.
94- log .severe (e .getMessage ());
105+ logger .severe (e .getMessage ());
95106
96107 throw new UnsatisfiedLinkError (e .getMessage ());
97108 }
@@ -130,13 +141,10 @@ public class GlobalScreen {
130141 }
131142 }
132143
133-
134- protected GlobalScreen () { }
135-
136144 /**
137145 * Adds the specified native key listener to receive key events from the
138- * native system. If listener is null, no exception is thrown and no action
139- * is performed.
146+ * native system.
147+ * If listener is null, no exception is thrown and no action is performed.
140148 *
141149 * @param listener a native key listener object
142150 */
@@ -149,8 +157,8 @@ public static void addNativeKeyListener(NativeKeyListener listener) {
149157 /**
150158 * Removes the specified native key listener so that it no longer receives
151159 * key events from the native system. This method performs no function if
152- * the listener specified by the argument was not previously added. If
153- * listener is null, no exception is thrown and no action is performed.
160+ * the listener specified by the argument was not previously added.I
161+ * If listener is null, no exception is thrown and no action is performed.
154162 *
155163 * @param listener a native key listener object
156164 */
@@ -162,8 +170,8 @@ public static void removeNativeKeyListener(NativeKeyListener listener) {
162170
163171 /**
164172 * Adds the specified native mouse listener to receive mouse events from the
165- * native system. If listener is null, no exception is thrown and no action
166- * is performed.
173+ * native system.
174+ * If listener is null, no exception is thrown and no action is performed.
167175 *
168176 * @param listener a native mouse listener object
169177 */
@@ -176,8 +184,8 @@ public static void addNativeMouseListener(NativeMouseListener listener) {
176184 /**
177185 * Removes the specified native mouse listener so that it no longer receives
178186 * mouse events from the native system. This method performs no function if
179- * the listener specified by the argument was not previously added. If
180- * listener is null, no exception is thrown and no action is performed.
187+ * the listener specified by the argument was not previously added.
188+ * If listener is null, no exception is thrown and no action is performed.
181189 *
182190 * @param listener a native mouse listener object
183191 */
@@ -189,8 +197,8 @@ public static void removeNativeMouseListener(NativeMouseListener listener) {
189197
190198 /**
191199 * Adds the specified native mouse motion listener to receive mouse motion
192- * events from the native system. If listener is null, no exception is
193- * thrown and no action is performed.
200+ * events from the native system.
201+ * If listener is null, no exception is thrown and no action is performed.
194202 *
195203 * @param listener a native mouse motion listener object
196204 */
@@ -204,8 +212,8 @@ public static void addNativeMouseMotionListener(NativeMouseMotionListener listen
204212 * Removes the specified native mouse motion listener so that it no longer
205213 * receives mouse motion events from the native system. This method performs
206214 * no function if the listener specified by the argument was not previously
207- * added. If listener is null, no exception is thrown and no action is
208- * performed.
215+ * added.
216+ * If listener is null, no exception is thrown and no action is performed.
209217 *
210218 * @param listener a native mouse motion listener object
211219 */
@@ -217,8 +225,7 @@ public static void removeNativeMouseMotionListener(NativeMouseMotionListener lis
217225
218226 /**
219227 * Adds the specified native mouse wheel listener to receive mouse wheel
220- * events from the native system. If listener is null, no exception is
221- * thrown and no action is performed.
228+ * events from the native system.
222229 *
223230 * @param listener a native mouse wheel listener object
224231 * @since 1.1
@@ -233,8 +240,8 @@ public static void addNativeMouseWheelListener(NativeMouseWheelListener listener
233240 * Removes the specified native mouse wheel listener so that it no longer
234241 * receives mouse wheel events from the native system. This method performs
235242 * no function if the listener specified by the argument was not previously
236- * added. If listener is null, no exception is thrown and no action is
237- * performed.
243+ * added.
244+ * If listener is null, no exception is thrown and no action is performed.
238245 *
239246 * @param listener a native mouse wheel listener object
240247 * @since 1.1
@@ -408,7 +415,7 @@ public static void registerNativeHook() throws NativeHookException {
408415 Thread .sleep (250 );
409416 }
410417 catch (InterruptedException e ) {
411- log .warning (e .getMessage ());
418+ logger .warning (e .getMessage ());
412419 break ;
413420 }
414421 }
@@ -420,6 +427,7 @@ public static void registerNativeHook() throws NativeHookException {
420427 hookThread = new NativeHookThread ();
421428
422429 synchronized (hookThread ) {
430+ loadLibrary ();
423431 hookThread .start ();
424432
425433 try {
0 commit comments