Skip to content

Commit

Permalink
Check for held down keys feature (#44)
Browse files Browse the repository at this point in the history
Add feature to check which keys are currently held down. Version bump to v3.6.
  • Loading branch information
Hellosager authored Apr 6, 2020
1 parent fc76916 commit 9b51385
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>lc.kra.system</groupId>
<artifactId>system-hook</artifactId>
<version>3.5</version>
<version>3.6</version>
<description>Global Keyboard / Mouse Hook for Java applications.</description>
<url>https://github.com/kristian/system-hook</url>

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/lc/kra/system/keyboard/GlobalKeyboardHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import static lc.kra.system.keyboard.event.GlobalKeyEvent.VK_RWIN;
import static lc.kra.system.keyboard.event.GlobalKeyEvent.VK_SHIFT;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -57,6 +59,9 @@ public class GlobalKeyboardHook {
private boolean menuPressed, shiftPressed, controlPressed, winPressed, extendedKey;

private List<GlobalKeyListener> listeners = new CopyOnWriteArrayList<GlobalKeyListener>();

private Set<Integer> heldDownKeyCodes = new HashSet<Integer>();

private Thread eventDispatcher = new Thread() {{
setName("Global Keyboard Hook Dispatcher");
setDaemon(true);
Expand Down Expand Up @@ -146,19 +151,52 @@ public GlobalKeyboardHook(GlobalHookMode mode) throws UnsatisfiedLinkError {
* @param event A global key event
*/
private void keyPressed(GlobalKeyEvent event) {
heldDownKeyCodes.add(event.getVirtualKeyCode());

for(GlobalKeyListener listener:listeners)
listener.keyPressed(event);
}

/**
* Invoke keyReleased (transition state TS_UP) for all registered listeners
*
* @param event A global key event
*/
private void keyReleased(GlobalKeyEvent event) {
heldDownKeyCodes.remove(event.getVirtualKeyCode());

for(GlobalKeyListener listener:listeners)
listener.keyReleased(event);
}

/**
* Checks if the specified key is currently held down
*
* @param virtualKeyCode the virtual code of the key, use constants in {@link GlobalKeyEvent}
*
* @return true if the key is currently held down
*/
public boolean isKeyHeldDown(int virtualKeyCode) {
return heldDownKeyCodes.contains(virtualKeyCode);
}


/**
* Checks if all the specified keys are currently held down
*
* @param virtualKeyCodes any number of specified key codes, use constants in {@link GlobalKeyEvent}
*
* @return true if all the specified keys are currently held down, false if any of the keys is not currently held down
*/
public boolean areKeysHeldDown(int... virtualKeyCodes) {
for(int keyCode : virtualKeyCodes) {
if(!isKeyHeldDown(keyCode)) {
return false;
}
}
return true;
}

/**
* Checks whether the keyboard hook is still alive and capturing inputs
*
Expand Down

0 comments on commit 9b51385

Please sign in to comment.