Skip to content

Commit

Permalink
Introduce key press limit
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Mar 6, 2024
1 parent 49eb06c commit b40d03d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public GameApp(ProcessHandle process, Game game) {

public void onStart() {
if (PdxuInstallation.getInstance().isNativeHookEnabled()) {
listener = new GameKeyListener(this);
listener = new GameKeyListener(this, 5000);
GlobalScreen.addNativeKeyListener(listener);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@
public class GameKeyListener implements NativeKeyListener {

private final GameApp handle;
private final long millisecondsGap;
private long lastCheck;
private int lastAction;

public GameKeyListener(GameApp handle) {
public GameKeyListener(GameApp handle, long millisecondsGap) {
this.handle = handle;
this.millisecondsGap = millisecondsGap;
}

public void nativeKeyPressed(NativeKeyEvent e) {
if ((e.getModifiers() & NativeKeyEvent.SHIFT_MASK) != 0 && ((e.getModifiers() & NativeKeyEvent.CTRL_MASK) != 0 || (e.getModifiers() & NativeKeyEvent.META_MASK) != 0)) {
if (e.getKeyCode() == NativeKeyEvent.VC_K) {
if (e.getKeyCode() == NativeKeyEvent.VC_K && canPass(1)) {
LoggerFactory.getLogger(GameKeyListener.class).debug("Kill key pressed");
handle.kill();
}
if (e.getKeyCode() == NativeKeyEvent.VC_I) {
if (e.getKeyCode() == NativeKeyEvent.VC_I && canPass(2)) {
LoggerFactory.getLogger(GameKeyListener.class).debug("Import key pressed");
GameAppManager.getInstance().playImportSound();
GameAppManager.getInstance().importLatest();
}
if (e.getKeyCode() == NativeKeyEvent.VC_C) {
if (e.getKeyCode() == NativeKeyEvent.VC_C && canPass(3)) {
LoggerFactory.getLogger(GameKeyListener.class).debug("Checkpoint key pressed");
GameAppManager.getInstance().loadLatestCheckpoint();
}
if (e.getKeyCode() == NativeKeyEvent.VC_R) {
if (e.getKeyCode() == NativeKeyEvent.VC_R && canPass(4)) {
TaskExecutor.getInstance().submitTask(() -> {
LoggerFactory.getLogger(GameKeyListener.class).debug("Reverting to latest save");
GameAppManager.getInstance().importLatestAndLaunch();
Expand All @@ -37,6 +41,21 @@ public void nativeKeyPressed(NativeKeyEvent e) {
}
}


private boolean canPass(int action) {
if (lastCheck == 0 || action != lastAction) {
lastAction = action;
lastCheck = System.currentTimeMillis();
return true;
}

var diff = System.currentTimeMillis() - lastCheck;
if (diff > millisecondsGap) {
lastCheck = System.currentTimeMillis();
}
return diff > millisecondsGap;
}

public void nativeKeyReleased(NativeKeyEvent e) {
}

Expand Down

0 comments on commit b40d03d

Please sign in to comment.