Class GlobalKeyboardHook


  • public class GlobalKeyboardHook
    extends Object
    • Constructor Detail

      • GlobalKeyboardHook

        public GlobalKeyboardHook()
                           throws UnsatisfiedLinkError
        Instantiate a new GlobalKeyboardHook. The constructor first tries to load the native library. On failure a UnsatisfiedLinkError is thrown. Afterwards the native keyboard hook is initialized. A RuntimeException is raised in case the hook could not be established. Two separate threads are started by the class. The HookThread and a separate EventDispatcherThread.
        Throws:
        UnsatisfiedLinkError - Thrown if loading the native library failed
        RuntimeException - Thrown if registering the low-level keyboard hook failed
      • GlobalKeyboardHook

        public GlobalKeyboardHook​(boolean raw)
                           throws UnsatisfiedLinkError
        Instantiate a new GlobalKeyboardHook.
        Parameters:
        raw - Use raw input, instead of a low-level system hook. Raw input will provide additional information of the device
        Throws:
        UnsatisfiedLinkError - Thrown if loading the native library failed
        RuntimeException - Thrown if registering the low-level keyboard hook failed
        See Also:
        GlobalKeyboardHook()
    • Method Detail

      • addKeyListener

        public void addKeyListener​(GlobalKeyListener listener)
        Adds a global key listener
        Parameters:
        listener - The listener to add
      • removeKeyListener

        public void removeKeyListener​(GlobalKeyListener listener)
        Removes a global key listener
        Parameters:
        listener - The listener to remove
      • isKeyHeldDown

        public boolean isKeyHeldDown​(int virtualKeyCode)
        Checks if the specified key is currently held down
        Parameters:
        virtualKeyCode - the virtual code of the key, use constants in GlobalKeyEvent
        Returns:
        true if the key is currently held down
      • areKeysHeldDown

        public boolean areKeysHeldDown​(int... virtualKeyCodes)
        Checks if all the specified keys are currently held down
        Parameters:
        virtualKeyCodes - any number of specified key codes, use constants in GlobalKeyEvent
        Returns:
        true if all the specified keys are currently held down, false if any of the keys is not currently held down
      • isAlive

        public boolean isAlive()
        Checks whether the keyboard hook is still alive and capturing inputs
        Returns:
        true if the keyboard hook is alive
      • shutdownHook

        public void shutdownHook()
        Shutdown the keyboard hook in case it is still alive. This method does nothing if the hook already shut down and will block until shut down.