Class 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)
    • Field Detail

      • log

        protected static Logger log
        Logging service for the native library.
      • eventExecutor

        protected static ExecutorService eventExecutor
        The service to dispatch events.
      • eventListeners

        protected static EventListenerList eventListeners
        The list of event listeners to notify.
    • Constructor Detail

      • GlobalScreen

        protected GlobalScreen()
    • 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 or Load "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()
        Returns true 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 a NativeInputEvent to the operating system's event queue. Each type of NativeInputEvent is processed according to its event id.

        For both NATIVE_KEY_PRESSED and NATIVE_KEY_RELEASED events, the virtual keycode and modifier mask are used in the creation of the native event. Please note that some platforms may generate NATIVE_KEY_PRESSED and NATIVE_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 a NATIVE_KEY_PRESSED followed by a NATIVE_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 the NativeKeyEvents. Both NATIVE_MOUSE_PRESSED and NATIVE_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 a NATIVE_MOUSE_PRESSED event followed by a NATIVE_MOUSE_RELEASED for the assigned event button.

        NATIVE_MOUSE_DRAGGED and NATIVE_MOUSE_MOVED events are handled identically. In order to produce a NATIVE_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 a NATIVE_MOUSE_MOVED event even if the event id was set to NATIVE_MOUSE_DRAGGED.

        NATIVE_MOUSE_WHEEL events are identical to NATIVE_MOUSE_PRESSED events. Wheel events will only produce pressed events and will never produce NATIVE_MOUSE_RELEASED, NATIVE_MOUSE_DRAGGED or NATIVE_MOUSE_MOVED

        Parameters:
        nativeEvent - the NativeInputEvent 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 an ExecutorService.

        Note: Using null as an ExecutorService will cause all delivered events to be discarded until a valid ExecutorService is set.

        Parameters:
        dispatcher - The ExecutorService used to dispatch native events.
        Since:
        2.0
        See Also:
        ExecutorService, Executors.newSingleThreadExecutor()