1
1
/* 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.
3
3
* https://github.com/kwhat/jnativehook/
4
4
*
5
5
* JNativeHook is free software: you can redistribute it and/or modify
9
9
*
10
10
* JNativeHook is distributed in the hope that it will be useful,
11
11
* 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
13
13
* GNU General Public License for more details.
14
14
*
15
15
* 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/>.
17
17
*/
18
18
package org .jnativehook ;
19
19
30
30
import java .io .File ;
31
31
import java .util .Iterator ;
32
32
import java .util .concurrent .ExecutorService ;
33
+ import java .util .logging .Level ;
33
34
import java .util .logging .Logger ;
34
35
35
36
/**
@@ -50,7 +51,13 @@ public class GlobalScreen {
50
51
/**
51
52
* Logging service for the native library.
52
53
*/
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 () { }
54
61
55
62
/**
56
63
* The service to control the hook.
@@ -67,7 +74,11 @@ public class GlobalScreen {
67
74
*/
68
75
protected static EventListenerList eventListeners = new EventListenerList ();
69
76
70
- static {
77
+ private static boolean loaded = false ;
78
+ private static void loadLibrary () {
79
+ if (loaded )
80
+ return ;
81
+ loaded = true ;
71
82
String libName = System .getProperty ("jnativehook.lib.name" , "JNativeHook" );
72
83
73
84
try {
@@ -91,7 +102,7 @@ public class GlobalScreen {
91
102
}
92
103
catch (Exception e ) {
93
104
// There was a problem instantiating the library loader.
94
- log .severe (e .getMessage ());
105
+ logger .severe (e .getMessage ());
95
106
96
107
throw new UnsatisfiedLinkError (e .getMessage ());
97
108
}
@@ -130,13 +141,10 @@ public class GlobalScreen {
130
141
}
131
142
}
132
143
133
-
134
- protected GlobalScreen () { }
135
-
136
144
/**
137
145
* 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.
140
148
*
141
149
* @param listener a native key listener object
142
150
*/
@@ -149,8 +157,8 @@ public static void addNativeKeyListener(NativeKeyListener listener) {
149
157
/**
150
158
* Removes the specified native key listener so that it no longer receives
151
159
* 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.
154
162
*
155
163
* @param listener a native key listener object
156
164
*/
@@ -162,8 +170,8 @@ public static void removeNativeKeyListener(NativeKeyListener listener) {
162
170
163
171
/**
164
172
* 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.
167
175
*
168
176
* @param listener a native mouse listener object
169
177
*/
@@ -176,8 +184,8 @@ public static void addNativeMouseListener(NativeMouseListener listener) {
176
184
/**
177
185
* Removes the specified native mouse listener so that it no longer receives
178
186
* 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.
181
189
*
182
190
* @param listener a native mouse listener object
183
191
*/
@@ -189,8 +197,8 @@ public static void removeNativeMouseListener(NativeMouseListener listener) {
189
197
190
198
/**
191
199
* 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.
194
202
*
195
203
* @param listener a native mouse motion listener object
196
204
*/
@@ -204,8 +212,8 @@ public static void addNativeMouseMotionListener(NativeMouseMotionListener listen
204
212
* Removes the specified native mouse motion listener so that it no longer
205
213
* receives mouse motion events from the native system. This method performs
206
214
* 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.
209
217
*
210
218
* @param listener a native mouse motion listener object
211
219
*/
@@ -217,8 +225,8 @@ public static void removeNativeMouseMotionListener(NativeMouseMotionListener lis
217
225
218
226
/**
219
227
* 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.
222
230
*
223
231
* @param listener a native mouse wheel listener object
224
232
* @since 1.1
@@ -233,8 +241,8 @@ public static void addNativeMouseWheelListener(NativeMouseWheelListener listener
233
241
* Removes the specified native mouse wheel listener so that it no longer
234
242
* receives mouse wheel events from the native system. This method performs
235
243
* 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.
238
246
*
239
247
* @param listener a native mouse wheel listener object
240
248
* @since 1.1
@@ -346,25 +354,25 @@ public NativeHookException getException() {
346
354
}
347
355
348
356
/**
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
351
359
* throwing code.
352
360
*
353
361
* @throws NativeHookException problem registering the native hook with the underlying operating system.
354
362
*/
355
363
protected native void enable () throws NativeHookException ;
356
364
357
365
/**
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.
359
367
*
360
368
* @throws NativeHookException problem un-registering the native hook with the underlying operating system.
361
369
*/
362
370
public native void disable () throws NativeHookException ;
363
371
364
372
/**
365
- * Dispatches an event to the appropriate processor. This method is
373
+ * Dispatches an event to the appropriate processor. This method is
366
374
* 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
368
376
* you would like to send events to other applications, please use
369
377
* {@link #postNativeEvent},
370
378
* <p>
@@ -408,7 +416,7 @@ public static void registerNativeHook() throws NativeHookException {
408
416
Thread .sleep (250 );
409
417
}
410
418
catch (InterruptedException e ) {
411
- log .warning (e .getMessage ());
419
+ logger .warning (e .getMessage ());
412
420
break ;
413
421
}
414
422
}
@@ -420,6 +428,7 @@ public static void registerNativeHook() throws NativeHookException {
420
428
hookThread = new NativeHookThread ();
421
429
422
430
synchronized (hookThread ) {
431
+ loadLibrary ();
423
432
hookThread .start ();
424
433
425
434
try {
@@ -480,36 +489,36 @@ public static boolean isNativeHookRegistered() {
480
489
*
481
490
* For both <code>NATIVE_KEY_PRESSED</code> and
482
491
* <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
484
493
* platforms may generate <code>NATIVE_KEY_PRESSED</code> and
485
494
* <code>NATIVE_KEY_RELEASED</code> events for each required modifier.
486
495
* <code>NATIVE_KEY_TYPED</code> events will first translate the associated
487
496
* keyChar to its respective virtual code and then produce a
488
497
* <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
490
499
* the keyChar to its respective virtual code, the event is ignored.
491
500
* <p>
492
501
*
493
502
* <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
495
504
* <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
497
506
* 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
499
508
* <code>NATIVE_MOUSE_PRESSED</code> event followed by a
500
509
* <code>NATIVE_MOUSE_RELEASED</code> for the assigned event button.
501
510
* <p>
502
511
*
503
512
* <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>
505
514
* 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
507
516
* <code>NATIVE_MOUSE_MOVED</code> event even if the event id was set to
508
517
* <code>NATIVE_MOUSE_DRAGGED</code>.
509
518
* <p>
510
519
*
511
520
* <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
513
522
* pressed events and will never produce <code>NATIVE_MOUSE_RELEASED</code>,
514
523
* <code>NATIVE_MOUSE_DRAGGED</code> or <code>NATIVE_MOUSE_MOVED</code>
515
524
*
@@ -666,9 +675,9 @@ private void processMouseWheelEvent(NativeMouseWheelEvent nativeEvent) {
666
675
}
667
676
668
677
/**
669
- * Set a different executor service for native event delivery. By default,
678
+ * Set a different executor service for native event delivery. By default,
670
679
* 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
672
681
* for event delivery by implementing an <code>ExecutorService</code>.
673
682
* <p>
674
683
* <b>Note:</b> Using null as an <code>ExecutorService</code> will cause all
0 commit comments