Skip to content

Commit da09e92

Browse files
author
Xerus
committed
Improve GlobalScreen
1 parent e8337e2 commit da09e92

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

src/java/org/jnativehook/GlobalScreen.java

+50-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* JNativeHook: Global keyboard and mouse hooking for Java.
2-
* Copyright (C) 2006-2018 Alexander Barker. All Rights Received.
2+
* Copyright (C) 2006-2018 Alexander Barker. All Rights Received.
33
* https://github.com/kwhat/jnativehook/
44
*
55
* JNativeHook is free software: you can redistribute it and/or modify
@@ -9,11 +9,11 @@
99
*
1010
* JNativeHook is distributed in the hope that it will be useful,
1111
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313
* GNU General Public License for more details.
1414
*
1515
* You should have received a copy of the GNU Lesser General Public License
16-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818
package org.jnativehook;
1919

@@ -30,6 +30,7 @@
3030
import java.io.File;
3131
import java.util.Iterator;
3232
import java.util.concurrent.ExecutorService;
33+
import java.util.logging.Level;
3334
import 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,8 @@ 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.
229+
* If listener is null, no exception is thrown and no action is performed.
222230
*
223231
* @param listener a native mouse wheel listener object
224232
* @since 1.1
@@ -233,8 +241,8 @@ public static void addNativeMouseWheelListener(NativeMouseWheelListener listener
233241
* Removes the specified native mouse wheel listener so that it no longer
234242
* receives mouse wheel events from the native system. This method performs
235243
* 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.
244+
* added.
245+
* If listener is null, no exception is thrown and no action is performed.
238246
*
239247
* @param listener a native mouse wheel listener object
240248
* @since 1.1
@@ -346,25 +354,25 @@ public NativeHookException getException() {
346354
}
347355

348356
/**
349-
* Native implementation to start the input hook. This method blocks and should only be called by this
350-
* specialized thread implementation. This method will notifyAll() after passing any exception exception
357+
* Native implementation to start the input hook. This method blocks and should only be called by this
358+
* specialized thread implementation. This method will notifyAll() after passing any exception exception
351359
* throwing code.
352360
*
353361
* @throws NativeHookException problem registering the native hook with the underlying operating system.
354362
*/
355363
protected native void enable() throws NativeHookException;
356364

357365
/**
358-
* Native implementation to stop the input hook. There is no other way to stop the hook.
366+
* Native implementation to stop the input hook. There is no other way to stop the hook.
359367
*
360368
* @throws NativeHookException problem un-registering the native hook with the underlying operating system.
361369
*/
362370
public native void disable() throws NativeHookException;
363371

364372
/**
365-
* Dispatches an event to the appropriate processor. This method is
373+
* Dispatches an event to the appropriate processor. This method is
366374
* generally called by the native library but may be used to synthesize
367-
* native events from Java without replaying them on the native system. If
375+
* native events from Java without replaying them on the native system. If
368376
* you would like to send events to other applications, please use
369377
* {@link #postNativeEvent},
370378
* <p>
@@ -408,7 +416,7 @@ public static void registerNativeHook() throws NativeHookException {
408416
Thread.sleep(250);
409417
}
410418
catch (InterruptedException e) {
411-
log.warning(e.getMessage());
419+
logger.warning(e.getMessage());
412420
break;
413421
}
414422
}
@@ -420,6 +428,7 @@ public static void registerNativeHook() throws NativeHookException {
420428
hookThread = new NativeHookThread();
421429

422430
synchronized (hookThread) {
431+
loadLibrary();
423432
hookThread.start();
424433

425434
try {
@@ -480,36 +489,36 @@ public static boolean isNativeHookRegistered() {
480489
*
481490
* For both <code>NATIVE_KEY_PRESSED</code> and
482491
* <code>NATIVE_KEY_RELEASED</code> events, the virtual keycode and modifier
483-
* mask are used in the creation of the native event. Please note that some
492+
* mask are used in the creation of the native event. Please note that some
484493
* platforms may generate <code>NATIVE_KEY_PRESSED</code> and
485494
* <code>NATIVE_KEY_RELEASED</code> events for each required modifier.
486495
* <code>NATIVE_KEY_TYPED</code> events will first translate the associated
487496
* keyChar to its respective virtual code and then produce a
488497
* <code>NATIVE_KEY_PRESSED</code> followed by a <code>NATIVE_KEY_RELEASED</code>
489-
* event using that virtual code. If the JNativeHook is unable to translate
498+
* event using that virtual code. If the JNativeHook is unable to translate
490499
* the keyChar to its respective virtual code, the event is ignored.
491500
* <p>
492501
*
493502
* <code>NativeMouseEvents</code> are processed in much the same way as the
494-
* <code>NativeKeyEvents</code>. Both <code>NATIVE_MOUSE_PRESSED</code> and
503+
* <code>NativeKeyEvents</code>. Both <code>NATIVE_MOUSE_PRESSED</code> and
495504
* <code>NATIVE_MOUSE_RELEASED</code> produce events corresponding to the
496-
* event's button code. Keyboard modifiers may be used in conjunction with
505+
* event's button code. Keyboard modifiers may be used in conjunction with
497506
* button press and release events, however, they might produce events for each
498-
* modifier. <code>NATIVE_MOUSE_CLICKED</code> events produce a
507+
* modifier. <code>NATIVE_MOUSE_CLICKED</code> events produce a
499508
* <code>NATIVE_MOUSE_PRESSED</code> event followed by a
500509
* <code>NATIVE_MOUSE_RELEASED</code> for the assigned event button.
501510
* <p>
502511
*
503512
* <code>NATIVE_MOUSE_DRAGGED</code> and <code>NATIVE_MOUSE_MOVED</code> events
504-
* are handled identically. In order to produce a <code>NATIVE_MOUSE_DRAGGED</code>
513+
* are handled identically. In order to produce a <code>NATIVE_MOUSE_DRAGGED</code>
505514
* event, you must specify a button modifier mask that contains at least one
506-
* button modifier and assign it to the event. Failure to do so will produce a
515+
* button modifier and assign it to the event. Failure to do so will produce a
507516
* <code>NATIVE_MOUSE_MOVED</code> event even if the event id was set to
508517
* <code>NATIVE_MOUSE_DRAGGED</code>.
509518
* <p>
510519
*
511520
* <code>NATIVE_MOUSE_WHEEL</code> events are identical to
512-
* <code>NATIVE_MOUSE_PRESSED</code> events. Wheel events will only produce
521+
* <code>NATIVE_MOUSE_PRESSED</code> events. Wheel events will only produce
513522
* pressed events and will never produce <code>NATIVE_MOUSE_RELEASED</code>,
514523
* <code>NATIVE_MOUSE_DRAGGED</code> or <code>NATIVE_MOUSE_MOVED</code>
515524
*
@@ -666,9 +675,9 @@ private void processMouseWheelEvent(NativeMouseWheelEvent nativeEvent) {
666675
}
667676

668677
/**
669-
* Set a different executor service for native event delivery. By default,
678+
* Set a different executor service for native event delivery. By default,
670679
* JNativeHook utilizes a single thread executor to dispatch events from
671-
* the native event queue. You may choose to use an alternative approach
680+
* the native event queue. You may choose to use an alternative approach
672681
* for event delivery by implementing an <code>ExecutorService</code>.
673682
* <p>
674683
* <b>Note:</b> Using null as an <code>ExecutorService</code> will cause all

0 commit comments

Comments
 (0)