Created
February 28, 2019 01:57
-
-
Save cnmoro/30e5769d89c940d9301aac6738670239 to your computer and use it in GitHub Desktop.
Java execute something at specific key press
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class keyboardInspectorThread extends Thread { | |
private static volatile boolean pausePressed = false; | |
public keyboardInspectorThread() { | |
} | |
public boolean isPausePressed() { | |
synchronized (keyboardInspectorThread.class) { | |
return pausePressed; | |
} | |
} | |
public void run() { | |
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() { | |
@Override | |
public boolean dispatchKeyEvent(KeyEvent ke) { | |
synchronized (keyboardInspectorThread.class) { | |
switch (ke.getID()) { | |
case KeyEvent.KEY_PRESSED: | |
if (ke.getKeyCode() == KeyEvent.VK_PAUSE) { | |
// ON PAUSE PRESSED | |
pausePressed = true; | |
} | |
break; | |
} | |
return false; | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment