From 97ad0ab478e7ab75ab8e78c9235961d2f2df6553 Mon Sep 17 00:00:00 2001 From: syeyoung Date: Tue, 11 Jan 2022 23:32:49 +0900 Subject: - Fix Keys repeating and re-implement key repeat mechanics using Keyboard.enableRepeatEvents (this breaks with current latest version of neu, but it should be fixed in neu's side) --- .../config/guiconfig/MParameterEdit.java | 8 +++---- .../java/kr/syeyoung/dungeonsguide/gui/MGui.java | 27 +++++++++++----------- .../java/kr/syeyoung/dungeonsguide/gui/MPanel.java | 16 ++++++------- .../dungeonsguide/gui/elements/MTextField.java | 14 ++--------- 4 files changed, 27 insertions(+), 38 deletions(-) (limited to 'src/main/java/kr') diff --git a/src/main/java/kr/syeyoung/dungeonsguide/config/guiconfig/MParameterEdit.java b/src/main/java/kr/syeyoung/dungeonsguide/config/guiconfig/MParameterEdit.java index 8ed0e028..6f791bcf 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/config/guiconfig/MParameterEdit.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/config/guiconfig/MParameterEdit.java @@ -177,15 +177,15 @@ public class MParameterEdit extends MPanel { } @Override - public void keyHeld0(char typedChar, int keyCode, long heldMS) { + public void keyHeld0(char typedChar, int keyCode) { if (isDisabled.test(featureParameter)) return; - super.keyHeld0(typedChar, keyCode, heldMS); + super.keyHeld0(typedChar, keyCode); } @Override - public void keyReleased0(char typedChar, int keyCode, long heldMS) { + public void keyReleased0(char typedChar, int keyCode) { if (isDisabled.test(featureParameter)) return; - super.keyReleased0(typedChar, keyCode, heldMS); + super.keyReleased0(typedChar, keyCode); } @Override diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java index 4f09a9be..13c9e00f 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java @@ -54,6 +54,7 @@ public class MGui extends GuiScreen { @Override public void initGui() { super.initGui(); + Keyboard.enableRepeatEvents(true); isOpen = true; ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft()); mainPanel.setBounds(new Rectangle(0,0,Minecraft.getMinecraft().displayWidth,Minecraft.getMinecraft().displayHeight)); @@ -93,17 +94,17 @@ public class MGui extends GuiScreen { e.printStackTrace(); } } - public void keyHeld(int keyCode, char typedChar, long heldMS) throws IOException { + public void keyHeld(int keyCode, char typedChar) throws IOException { try { - mainPanel.keyHeld0(typedChar, keyCode, heldMS); + mainPanel.keyHeld0(typedChar, keyCode); } catch (Throwable e) { if (e.getMessage() == null || !e.getMessage().contains("hack to stop")) e.printStackTrace(); } } - public void keyReleased(int keyCode, char typedChar,long heldMS) throws IOException { + public void keyReleased(int keyCode, char typedChar) throws IOException { try { - mainPanel.keyReleased0(typedChar, keyCode, heldMS); + mainPanel.keyReleased0(typedChar, keyCode); } catch (Throwable e) { if (e.getMessage() == null || !e.getMessage().contains("hack to stop")) e.printStackTrace(); @@ -125,8 +126,8 @@ public class MGui extends GuiScreen { @Override public void onGuiClosed() { super.onGuiClosed(); + Keyboard.enableRepeatEvents(false); isOpen = false; - keyLastPressed.clear(); try { Mouse.setNativeCursor(null); @@ -235,17 +236,18 @@ public class MGui extends GuiScreen { e.printStackTrace(); } } - private Map> keyLastPressed = new HashMap<>(); public void handleKeyboardInput() throws IOException { if (!isOpen) return; + if (Keyboard.getEventKeyState()) { - keyLastPressed.put(Keyboard.getEventKey(), new Tuple<>(Keyboard.getEventCharacter(), System.currentTimeMillis())); - this.keyTyped(Keyboard.getEventCharacter(), Keyboard.getEventKey()); + if (Keyboard.isRepeatEvent()) + this.keyHeld(Keyboard.getEventKey(), Keyboard.getEventCharacter()); + else + this.keyTyped(Keyboard.getEventCharacter(), Keyboard.getEventKey()); } else { - Tuple val = keyLastPressed.remove(Keyboard.getEventKey()); - if (val != null) this.keyReleased(Keyboard.getEventKey(), val.getFirst(), val.getSecond()); + this.keyReleased(Keyboard.getEventKey(), Keyboard.getEventCharacter()); } this.mc.dispatchKeypresses(); @@ -253,10 +255,7 @@ public class MGui extends GuiScreen { @Override public void handleInput() throws IOException { +// Keyboard.enableRepeatEvents(true); // i hope it's temproary solution super.handleInput(); - long currentTime = System.currentTimeMillis(); - for (Map.Entry> integerLongEntry : keyLastPressed.entrySet()) { - this.keyHeld(integerLongEntry.getKey(), integerLongEntry.getValue().getFirst(), integerLongEntry.getValue().getSecond()); - } } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java index c71f8312..b79ec148 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java @@ -200,24 +200,24 @@ public class MPanel { keyPressed(typedChar, keyCode); } public void keyPressed(char typedChar, int keyCode) {} - public void keyHeld0(char typedChar, int keyCode, long heldMS) { + public void keyHeld0(char typedChar, int keyCode) { for (MPanel childComponent : getChildComponents()) { - childComponent.keyHeld0(typedChar, keyCode, heldMS); + childComponent.keyHeld0(typedChar, keyCode); } if (isFocused) - keyHeld(typedChar, keyCode, heldMS); + keyHeld(typedChar, keyCode); } - public void keyHeld(char typedChar, int keyCode, long heldMS) {} - public void keyReleased0(char typedChar, int keyCode, long heldMS) { + public void keyHeld(char typedChar, int keyCode) {} + public void keyReleased0(char typedChar, int keyCode) { for (MPanel childComponent : getChildComponents()) { - childComponent.keyReleased0(typedChar, keyCode, heldMS); + childComponent.keyReleased0(typedChar, keyCode); } if (isFocused) - keyReleased(typedChar, keyCode, heldMS); + keyReleased(typedChar, keyCode); } - public void keyReleased(char typedChar, int keyCode, long heldMS) {} + public void keyReleased(char typedChar, int keyCode) {} public boolean mouseClicked0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int mouseButton) { diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java index 653cd2d9..d87c08e3 100755 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java @@ -205,25 +205,15 @@ public class MTextField extends MPanel { } } - private Map callKeyHeldIfAfter = new HashMap<>(); @Override - public void keyHeld(char typedChar, int keyCode, long heldMS) { + public void keyHeld(char typedChar, int keyCode) { if (!isFocused) return; - Long callAfter = callKeyHeldIfAfter.get(keyCode); - if (callAfter == null) return; - if (callAfter <= System.currentTimeMillis()) this.keyPressed(typedChar, keyCode); - } - - @Override - public void keyReleased(char typedChar, int keyCode, long heldMS) { - callKeyHeldIfAfter.remove(keyCode); + this.keyPressed(typedChar, keyCode); } @Override public void keyPressed(char typedChar, int keycode) { if (!isFocused) return; - if (callKeyHeldIfAfter.containsKey(keycode)) callKeyHeldIfAfter.put(keycode, System.currentTimeMillis()+50); - else callKeyHeldIfAfter.put(keycode, System.currentTimeMillis() + 750); if (selectionStart == -1) { if (keycode == 199) { // home -- cgit