- 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 class
GlobalScreen.NativeHookThread
Specialized thread implementation for the native hook.
-
Field Summary
Fields Modifier and Type Field Description protected static ExecutorService
eventExecutor
The service to dispatch events.protected static EventListenerList
eventListeners
The list of event listeners to notify.protected static GlobalScreen.NativeHookThread
hookThread
The service to control the hook.protected static Logger
log
Logging service for the native library.
-
Constructor Summary
Constructors Modifier Constructor Description protected
GlobalScreen()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addNativeKeyListener(NativeKeyListener listener)
Adds the specified native key listener to receive key events from the native system.static void
addNativeMouseListener(NativeMouseListener listener)
Adds the specified native mouse listener to receive mouse events from the native system.static void
addNativeMouseMotionListener(NativeMouseMotionListener listener)
Adds the specified native mouse motion listener to receive mouse motion events from the native system.static void
addNativeMouseWheelListener(NativeMouseWheelListener listener)
Adds the specified native mouse wheel listener to receive mouse wheel events from the native system.static Integer
getAutoRepeatDelay()
Retrieves the keyboard auto repeat delay.static Integer
getAutoRepeatRate()
Retrieves the keyboard auto repeat rate.static Integer
getMultiClickIterval()
Retrieves the double/triple click interval in milliseconds.static NativeMonitorInfo[]
getNativeMonitors()
Get information about the native monitor configuration and layout.static Integer
getPointerAccelerationMultiplier()
Retrieves the mouse acceleration multiplier.static Integer
getPointerAccelerationThreshold()
Retrieves the mouse acceleration threshold.static Integer
getPointerSensitivity()
Retrieves the mouse sensitivity.static boolean
isNativeHookRegistered()
Returnstrue
if the native hook is currently registered.static void
postNativeEvent(NativeInputEvent nativeEvent)
Add aNativeInputEvent
to the operating system's event queue.static void
registerNativeHook()
Enable the native hook.static void
removeNativeKeyListener(NativeKeyListener listener)
Removes the specified native key listener so that it no longer receives key events from the native system.static void
removeNativeMouseListener(NativeMouseListener listener)
Removes the specified native mouse listener so that it no longer receives mouse events from the native system.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.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.static void
setEventDispatcher(ExecutorService dispatcher)
Set a different executor service for native event delivery.static void
unregisterNativeHook()
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
NativeMonitorInfo
objects. - Since:
- 2.1
-
getAutoRepeatRate
public static Integer getAutoRepeatRate()
Retrieves the keyboard auto repeat rate.- Returns:
Integer
or null.- Since:
- 2.1
-
getAutoRepeatDelay
public static Integer getAutoRepeatDelay()
Retrieves the keyboard auto repeat delay.- Returns:
Integer
or null.- Since:
- 2.1
-
getPointerAccelerationMultiplier
public static Integer getPointerAccelerationMultiplier()
Retrieves the mouse acceleration multiplier.- Returns:
Integer
or null.- Since:
- 2.1
-
getPointerAccelerationThreshold
public static Integer getPointerAccelerationThreshold()
Retrieves the mouse acceleration threshold.- Returns:
Integer
or null.- Since:
- 2.1
-
getPointerSensitivity
public static Integer getPointerSensitivity()
Retrieves the mouse sensitivity.- Returns:
Integer
or null.- Since:
- 2.1
-
getMultiClickIterval
public static Integer getMultiClickIterval()
Retrieves the double/triple click interval in milliseconds.- Returns:
Integer
or null.- Since:
- 2.1
-
registerNativeHook
public static void registerNativeHook() throws NativeHookException
Enable the native hook. If the hooks is currently enabled, this function has no effect.Note: This method will throw a
NativeHookException
if 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 NativeHookException
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()
Returnstrue
if 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 aNativeInputEvent
to the operating system's event queue. Each type ofNativeInputEvent
is processed according to its event id.For both
NATIVE_KEY_PRESSED
andNATIVE_KEY_RELEASED
events, the virtual keycode and modifier mask are used in the creation of the native event. Please note that some platforms may generateNATIVE_KEY_PRESSED
andNATIVE_KEY_RELEASED
events for each required modifier.NATIVE_KEY_TYPED
events will first translate the associated keyChar to its respective virtual code and then produce aNATIVE_KEY_PRESSED
followed by aNATIVE_KEY_RELEASED
event using that virtual code. If the JNativeHook is unable to translate the keyChar to its respective virtual code, the event is ignored.NativeMouseEvents
are processed in much the same way as theNativeKeyEvents
. BothNATIVE_MOUSE_PRESSED
andNATIVE_MOUSE_RELEASED
produce 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_CLICKED
events produce aNATIVE_MOUSE_PRESSED
event followed by aNATIVE_MOUSE_RELEASED
for the assigned event button.NATIVE_MOUSE_DRAGGED
andNATIVE_MOUSE_MOVED
events are handled identically. In order to produce aNATIVE_MOUSE_DRAGGED
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 aNATIVE_MOUSE_MOVED
event even if the event id was set toNATIVE_MOUSE_DRAGGED
.NATIVE_MOUSE_WHEEL
events are identical toNATIVE_MOUSE_PRESSED
events. Wheel events will only produce pressed events and will never produceNATIVE_MOUSE_RELEASED
,NATIVE_MOUSE_DRAGGED
orNATIVE_MOUSE_MOVED
- Parameters:
nativeEvent
- theNativeInputEvent
sent 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
ExecutorService
will cause all delivered events to be discarded until a validExecutorService
is set.- Parameters:
dispatcher
- TheExecutorService
used to dispatch native events.- Since:
- 2.0
- See Also:
ExecutorService
,Executors.newSingleThreadExecutor()
-
-