Package lc.kra.system.keyboard
Class GlobalKeyboardHook
- java.lang.Object
-
- lc.kra.system.keyboard.GlobalKeyboardHook
-
public class GlobalKeyboardHook extends Object
-
-
Constructor Summary
Constructors Constructor Description GlobalKeyboardHook()Instantiate a new GlobalKeyboardHook.GlobalKeyboardHook(boolean raw)Instantiate a new GlobalKeyboardHook.GlobalKeyboardHook(GlobalHookMode mode)Instantiate a new GlobalKeyboardHook.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddKeyListener(GlobalKeyListener listener)Adds a global key listenerbooleanareKeysHeldDown(int... virtualKeyCodes)Checks if all the specified keys are currently held downbooleanisAlive()Checks whether the keyboard hook is still alive and capturing inputsbooleanisKeyHeldDown(int virtualKeyCode)Checks if the specified key is currently held downstatic Map<Long,String>listKeyboards()Lists all connected keyboardsvoidremoveKeyListener(GlobalKeyListener listener)Removes a global key listenervoidshutdownHook()Shutdown the keyboard hook in case it is still alive.
-
-
-
Constructor Detail
-
GlobalKeyboardHook
public GlobalKeyboardHook() throws UnsatisfiedLinkErrorInstantiate a new GlobalKeyboardHook. The constructor first tries to load the native library. On failure aUnsatisfiedLinkErroris thrown. Afterwards the native keyboard hook is initialized. ARuntimeExceptionis 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 failedRuntimeException- Thrown if registering the low-level keyboard hook failed
-
GlobalKeyboardHook
public GlobalKeyboardHook(boolean raw) throws UnsatisfiedLinkErrorInstantiate 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 failedRuntimeException- Thrown if registering the low-level keyboard hook failed- See Also:
GlobalKeyboardHook()
-
GlobalKeyboardHook
public GlobalKeyboardHook(GlobalHookMode mode) throws UnsatisfiedLinkError
Instantiate a new GlobalKeyboardHook.- Parameters:
mode- The mode to capture the input- Throws:
UnsatisfiedLinkError- Thrown if loading the native library failedRuntimeException- 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 inGlobalKeyEvent- 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 inGlobalKeyEvent- 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.
-
listKeyboards
public static Map<Long,String> listKeyboards() throws UnsatisfiedLinkError
Lists all connected keyboards- Returns:
- A map of device handles and display names
- Throws:
UnsatisfiedLinkError
-
-