Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
Allow ctrl c as keybind. Fix callbacks firing more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Jan 23, 2020
1 parent db73192 commit ccc87a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.tricht.lunaris.listeners;

import dev.tricht.lunaris.item.ItemGrabber;
import dev.tricht.lunaris.util.Platform;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
Expand All @@ -15,8 +16,6 @@ public class ClipboardListenerStack implements GameListener, ClipboardOwner {
private final ItemGrabber itemGrabber;
private final Robot robot;

private FlavorListener currentFlavorListener = null;

private static boolean isOwner = false;
private static ClipboardCallback callback;

Expand All @@ -36,15 +35,20 @@ public void addListener(GameListener listener) {
public void onEvent(GameEvent event) {
for (GameListener listenerEntry : keyListeners) {
if (listenerEntry.supports(event)) {
if (currentFlavorListener != null) {
Toolkit.getDefaultToolkit().getSystemClipboard().removeFlavorListener(currentFlavorListener);
}
if (!isOwner) {
flushClipboard();
}
callback = (String clipboard) -> {
event.setItem(itemGrabber.grab(clipboard));
listenerEntry.onEvent(event);
callback = new ClipboardCallback() {
private boolean fired = false;
@Override
public void onContentChange(String clipboard) {
if (clipboard.isEmpty() || fired) {
return;
}
event.setItem(itemGrabber.grab(clipboard));
listenerEntry.onEvent(event);
fired = true;
}
};
log.debug("Registered clipboard listener for " + listenerEntry.getClass().getName());
pressControlC(event);
Expand All @@ -65,6 +69,9 @@ public boolean supports(GameEvent event) {

private void registerClipboardListener() {
Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(flavorEvent -> {
if (!Platform.isPoeActive()) {
return;
}
String clipboardText = getClipboardText();
log.debug("Got clipboard text:" + clipboardText);
if (clipboardText != null && callback != null) {
Expand Down Expand Up @@ -99,6 +106,7 @@ private String getClipboardText() {
}

private void pressControlC(GameEvent event) {
HotKeyHandler.setPaused(true);
boolean wasControlAlreadyPressed = (event.getOriginalEvent().getModifiers() & 34) != 0;

if (!wasControlAlreadyPressed) {
Expand All @@ -111,6 +119,7 @@ private void pressControlC(GameEvent event) {
robot.keyRelease(KeyEvent.VK_CONTROL);
}
robot.keyRelease(KeyEvent.VK_C);
HotKeyHandler.setPaused(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.tricht.lunaris.util.Platform;
import dev.tricht.lunaris.tooltip.TooltipCreator;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
Expand All @@ -20,6 +21,9 @@
@Slf4j
public class HotKeyHandler implements NativeKeyListener, NativeMouseInputListener, NativeMouseWheelListener {

@Setter
private static boolean isPaused = false;

private Point position;

private ArrayList<GameListener> keyListeners = new ArrayList<>();
Expand All @@ -44,7 +48,10 @@ public void setRespondTo(ArrayList<KeyCombo> combos) {
}

public boolean shouldRespond(NativeKeyEvent event) {
for(KeyCombo combo : listenCombos) {
if (isPaused) {
return false;
}
for (KeyCombo combo : listenCombos) {
if (combo.matches(event)) {
return true;
}
Expand Down

0 comments on commit ccc87a2

Please sign in to comment.