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 da09e92
Showing 1 changed file with 50 additions and 41 deletions.
91 changes: 50 additions & 41 deletions src/java/org/jnativehook/GlobalScreen.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jnativehook;

Expand All @@ -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,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
Expand All @@ -233,8 +241,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 @@ -346,25 +354,25 @@ 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.
*/
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},
* <p>
Expand Down Expand Up @@ -408,7 +416,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 +428,7 @@ public static void registerNativeHook() throws NativeHookException {
hookThread = new NativeHookThread();

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

try {
Expand Down Expand Up @@ -480,36 +489,36 @@ public static boolean isNativeHookRegistered() {
*
* For both <code>NATIVE_KEY_PRESSED</code> and
* <code>NATIVE_KEY_RELEASED</code> 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 <code>NATIVE_KEY_PRESSED</code> and
* <code>NATIVE_KEY_RELEASED</code> events for each required modifier.
* <code>NATIVE_KEY_TYPED</code> events will first translate the associated
* keyChar to its respective virtual code and then produce a
* <code>NATIVE_KEY_PRESSED</code> followed by a <code>NATIVE_KEY_RELEASED</code>
* 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.
* <p>
*
* <code>NativeMouseEvents</code> are processed in much the same way as the
* <code>NativeKeyEvents</code>. Both <code>NATIVE_MOUSE_PRESSED</code> and
* <code>NativeKeyEvents</code>. Both <code>NATIVE_MOUSE_PRESSED</code> and
* <code>NATIVE_MOUSE_RELEASED</code> 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. <code>NATIVE_MOUSE_CLICKED</code> events produce a
* modifier. <code>NATIVE_MOUSE_CLICKED</code> events produce a
* <code>NATIVE_MOUSE_PRESSED</code> event followed by a
* <code>NATIVE_MOUSE_RELEASED</code> for the assigned event button.
* <p>
*
* <code>NATIVE_MOUSE_DRAGGED</code> and <code>NATIVE_MOUSE_MOVED</code> events
* are handled identically. In order to produce a <code>NATIVE_MOUSE_DRAGGED</code>
* are handled identically. In order to produce a <code>NATIVE_MOUSE_DRAGGED</code>
* 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
* <code>NATIVE_MOUSE_MOVED</code> event even if the event id was set to
* <code>NATIVE_MOUSE_DRAGGED</code>.
* <p>
*
* <code>NATIVE_MOUSE_WHEEL</code> events are identical to
* <code>NATIVE_MOUSE_PRESSED</code> events. Wheel events will only produce
* <code>NATIVE_MOUSE_PRESSED</code> events. Wheel events will only produce
* pressed events and will never produce <code>NATIVE_MOUSE_RELEASED</code>,
* <code>NATIVE_MOUSE_DRAGGED</code> or <code>NATIVE_MOUSE_MOVED</code>
*
Expand Down Expand Up @@ -666,9 +675,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 <code>ExecutorService</code>.
* <p>
* <b>Note:</b> Using null as an <code>ExecutorService</code> will cause all
Expand Down

0 comments on commit da09e92

Please sign in to comment.