Skip to content

Instantly share code, notes, and snippets.

@cnmoro
Created February 28, 2019 01:57
Show Gist options
  • Save cnmoro/30e5769d89c940d9301aac6738670239 to your computer and use it in GitHub Desktop.
Save cnmoro/30e5769d89c940d9301aac6738670239 to your computer and use it in GitHub Desktop.
Java execute something at specific key press
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