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 ClassesModifier and TypeClassDescriptionprotected static classSpecialized thread implementation for the native hook. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static ExecutorServiceThe service to dispatch events.protected static EventListenerListThe list of event listeners to notify.protected static GlobalScreen.NativeHookThreadThe service to control the hook.protected static LoggerLogging service for the native library. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 voidAdds the specified native mouse motion listener to receive mouse motion events from the native system.static voidAdds the specified native mouse wheel listener to receive mouse wheel events from the native system.static IntegerRetrieves the keyboard auto repeat delay.static IntegerRetrieves the keyboard auto repeat rate.static IntegerRetrieves the double/triple click interval in milliseconds.static NativeMonitorInfo[]Get information about the native monitor configuration and layout.static IntegerRetrieves the mouse acceleration multiplier.static IntegerRetrieves the mouse acceleration threshold.static IntegerRetrieves the mouse sensitivity.static booleanReturnstrueif the native hook is currently registered.static voidpostNativeEvent(NativeInputEvent nativeEvent) Add aNativeInputEventto the operating system's event queue.static voidEnable 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 voidRemoves the specified native mouse motion listener so that it no longer receives mouse motion events from the native system.static voidRemoves 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 voidDisable the native hook if it is currently registered.
-
Field Details
-
log
Logging service for the native library. -
hookThread
The service to control the hook. -
eventExecutor
The service to dispatch events. -
eventListeners
The list of event listeners to notify.
-
-
Constructor Details
-
GlobalScreen
protected GlobalScreen()Disable direct object construction.
-
-
Method Details
-
addNativeKeyListener
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
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
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
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
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
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
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
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
Get information about the native monitor configuration and layout.- Returns:
- an array of
NativeMonitorInfoobjects. - Since:
- 2.1
-
getAutoRepeatRate
Retrieves the keyboard auto repeat rate.- Returns:
Integeror null.- Since:
- 2.1
-
getAutoRepeatDelay
Retrieves the keyboard auto repeat delay.- Returns:
Integeror null.- Since:
- 2.1
-
getPointerAccelerationMultiplier
Retrieves the mouse acceleration multiplier.- Returns:
Integeror null.- Since:
- 2.1
-
getPointerAccelerationThreshold
Retrieves the mouse acceleration threshold.- Returns:
Integeror null.- Since:
- 2.1
-
getPointerSensitivity
Retrieves the mouse sensitivity.- Returns:
Integeror null.- Since:
- 2.1
-
getMultiClickIterval
Retrieves the double/triple click interval in milliseconds.- Returns:
Integeror null.- Since:
- 2.1
-
registerNativeHook
Enable 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
Disable 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
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
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:
-