- java.lang.Object
-
- com.github.kwhat.jnativehook.GlobalScreen
-
public class GlobalScreen extends Object
GlobalScreen is used to represent the native screen area that Java does not usually have access to. This class can be thought of as the source component for native input events.This class also handles the loading, unpacking and communication with the native library. That includes registering and un-registering the native hook with the underlying operating system and adding global keyboard and mouse listeners.
- Since:
- 1.0
- Version:
- 2.1
- Author:
- Alexander Barker (alex@1stleg.com)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classGlobalScreen.NativeHookThreadSpecialized thread implementation for the native hook.
-
Field Summary
Fields Modifier and Type Field Description protected static ExecutorServiceeventExecutorThe service to dispatch events.protected static EventListenerListeventListenersThe list of event listeners to notify.protected static GlobalScreen.NativeHookThreadhookThreadThe service to control the hook.protected static LoggerlogLogging service for the native library.
-
Constructor Summary
Constructors Modifier Constructor Description protectedGlobalScreen()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddNativeKeyListener(NativeKeyListener listener)Adds the specified native key listener to receive key events from the native system.static voidaddNativeMouseListener(NativeMouseListener listener)Adds the specified native mouse listener to receive mouse events from the native system.static voidaddNativeMouseMotionListener(NativeMouseMotionListener listener)Adds the specified native mouse motion listener to receive mouse motion events from the native system.static voidaddNativeMouseWheelListener(NativeMouseWheelListener listener)Adds the specified native mouse wheel listener to receive mouse wheel events from the native system.static IntegergetAutoRepeatDelay()Retrieves the keyboard auto repeat delay.static IntegergetAutoRepeatRate()Retrieves the keyboard auto repeat rate.static IntegergetMultiClickIterval()Retrieves the double/triple click interval in milliseconds.static NativeMonitorInfo[]getNativeMonitors()Get information about the native monitor configuration and layout.static IntegergetPointerAccelerationMultiplier()Retrieves the mouse acceleration multiplier.static IntegergetPointerAccelerationThreshold()Retrieves the mouse acceleration threshold.static IntegergetPointerSensitivity()Retrieves the mouse sensitivity.static booleanisNativeHookRegistered()Returnstrueif the native hook is currently registered.static voidpostNativeEvent(NativeInputEvent nativeEvent)Add aNativeInputEventto the operating system's event queue.static voidregisterNativeHook()Enable the native hook.static voidremoveNativeKeyListener(NativeKeyListener listener)Removes the specified native key listener so that it no longer receives key events from the native system.static voidremoveNativeMouseListener(NativeMouseListener listener)Removes the specified native mouse listener so that it no longer receives mouse events from the native system.static voidremoveNativeMouseMotionListener(NativeMouseMotionListener listener)Removes the specified native mouse motion listener so that it no longer receives mouse motion events from the native system.static voidremoveNativeMouseWheelListener(NativeMouseWheelListener listener)Removes the specified native mouse wheel listener so that it no longer receives mouse wheel events from the native system.static voidsetEventDispatcher(ExecutorService dispatcher)Set a different executor service for native event delivery.static voidunregisterNativeHook()Disable the native hook if it is currently registered.
-
-
-
Field Detail
-
log
protected static Logger log
Logging service for the native library.
-
hookThread
protected static GlobalScreen.NativeHookThread hookThread
The service to control the hook.
-
eventExecutor
protected static ExecutorService eventExecutor
The service to dispatch events.
-
eventListeners
protected static EventListenerList eventListeners
The list of event listeners to notify.
-
-
Method Detail
-
addNativeKeyListener
public static void addNativeKeyListener(NativeKeyListener listener)
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.- Parameters:
listener- a native key listener object
-
removeNativeKeyListener
public static void removeNativeKeyListener(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.- Parameters:
listener- a native key listener object
-
addNativeMouseListener
public static void addNativeMouseListener(NativeMouseListener 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.- Parameters:
listener- a native mouse listener object
-
removeNativeMouseListener
public static void removeNativeMouseListener(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.- Parameters:
listener- a native mouse listener object
-
addNativeMouseMotionListener
public static void addNativeMouseMotionListener(NativeMouseMotionListener 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.- Parameters:
listener- a native mouse motion listener object
-
removeNativeMouseMotionListener
public static void removeNativeMouseMotionListener(NativeMouseMotionListener listener)
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.- Parameters:
listener- a native mouse motion listener object
-
addNativeMouseWheelListener
public static void addNativeMouseWheelListener(NativeMouseWheelListener listener)
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.- Parameters:
listener- a native mouse wheel listener object- Since:
- 1.1
-
removeNativeMouseWheelListener
public static void removeNativeMouseWheelListener(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.- Parameters:
listener- a native mouse wheel listener object- Since:
- 1.1
-
getNativeMonitors
public static NativeMonitorInfo[] getNativeMonitors()
Get information about the native monitor configuration and layout.- Returns:
- an array of
NativeMonitorInfoobjects. - Since:
- 2.1
-
getAutoRepeatRate
public static Integer getAutoRepeatRate()
Retrieves the keyboard auto repeat rate.- Returns:
Integeror null.- Since:
- 2.1
-
getAutoRepeatDelay
public static Integer getAutoRepeatDelay()
Retrieves the keyboard auto repeat delay.- Returns:
Integeror null.- Since:
- 2.1
-
getPointerAccelerationMultiplier
public static Integer getPointerAccelerationMultiplier()
Retrieves the mouse acceleration multiplier.- Returns:
Integeror null.- Since:
- 2.1
-
getPointerAccelerationThreshold
public static Integer getPointerAccelerationThreshold()
Retrieves the mouse acceleration threshold.- Returns:
Integeror null.- Since:
- 2.1
-
getPointerSensitivity
public static Integer getPointerSensitivity()
Retrieves the mouse sensitivity.- Returns:
Integeror null.- Since:
- 2.1
-
getMultiClickIterval
public static Integer getMultiClickIterval()
Retrieves the double/triple click interval in milliseconds.- Returns:
Integeror null.- Since:
- 2.1
-
registerNativeHook
public static void registerNativeHook() throws NativeHookExceptionEnable the native hook. If the hooks is currently enabled, this function has no effect.Note: This method will throw a
NativeHookExceptionif specific operating system feature is unavailable or disabled. For example: Access for assistive devices is unchecked in the Universal Access section of the System Preferences on Apple's OS X platform orLoad "record"is missing for the xorg.conf file on Unix/Linux/Solaris platforms.- Throws:
NativeHookException- problem registering the native hook with the underlying operating system.- Since:
- 1.1
-
unregisterNativeHook
public static void unregisterNativeHook() throws NativeHookExceptionDisable the native hook if it is currently registered. If the native hook it is not registered the function has no effect.- Throws:
NativeHookException- hook interrupted by Java.- Since:
- 1.1
-
isNativeHookRegistered
public static boolean isNativeHookRegistered()
Returnstrueif the native hook is currently registered.- Returns:
- true if the native hook is currently registered.
- Since:
- 1.1
-
postNativeEvent
public static void postNativeEvent(NativeInputEvent nativeEvent)
Add aNativeInputEventto the operating system's event queue. Each type ofNativeInputEventis processed according to its event id.For both
NATIVE_KEY_PRESSEDandNATIVE_KEY_RELEASEDevents, the virtual keycode and modifier mask are used in the creation of the native event. Please note that some platforms may generateNATIVE_KEY_PRESSEDandNATIVE_KEY_RELEASEDevents for each required modifier.NATIVE_KEY_TYPEDevents will first translate the associated keyChar to its respective virtual code and then produce aNATIVE_KEY_PRESSEDfollowed by aNATIVE_KEY_RELEASEDevent using that virtual code. If the JNativeHook is unable to translate the keyChar to its respective virtual code, the event is ignored.NativeMouseEventsare processed in much the same way as theNativeKeyEvents. BothNATIVE_MOUSE_PRESSEDandNATIVE_MOUSE_RELEASEDproduce events corresponding to the 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.NATIVE_MOUSE_CLICKEDevents produce aNATIVE_MOUSE_PRESSEDevent followed by aNATIVE_MOUSE_RELEASEDfor the assigned event button.NATIVE_MOUSE_DRAGGEDandNATIVE_MOUSE_MOVEDevents are handled identically. In order to produce aNATIVE_MOUSE_DRAGGEDevent, 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 aNATIVE_MOUSE_MOVEDevent even if the event id was set toNATIVE_MOUSE_DRAGGED.NATIVE_MOUSE_WHEELevents are identical toNATIVE_MOUSE_PRESSEDevents. Wheel events will only produce pressed events and will never produceNATIVE_MOUSE_RELEASED,NATIVE_MOUSE_DRAGGEDorNATIVE_MOUSE_MOVED- Parameters:
nativeEvent- theNativeInputEventsent to the native system.- Since:
- 2.0
-
setEventDispatcher
public static void setEventDispatcher(ExecutorService dispatcher)
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 for event delivery by implementing anExecutorService.Note: Using null as an
ExecutorServicewill cause all delivered events to be discarded until a validExecutorServiceis set.- Parameters:
dispatcher- TheExecutorServiceused to dispatch native events.- Since:
- 2.0
- See Also:
ExecutorService,Executors.newSingleThreadExecutor()
-
-