diff --git a/src/main/java/meteordevelopment/meteorclient/gui/WidgetScreen.java b/src/main/java/meteordevelopment/meteorclient/gui/WidgetScreen.java index f38ca464863..1b3048169a9 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/WidgetScreen.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/WidgetScreen.java @@ -184,20 +184,24 @@ public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmou } @Override - public boolean keyReleased(@NonNull KeyEvent input) { + public boolean keyReleased(@NonNull KeyEvent event) { if (locked) return false; - if ((input.modifiers() == MOD_CONTROL || input.modifiers() == MOD_SUPER) && input.key() == KEY_9) { + boolean ctrl = SystemUtils.IS_OS_MAC + ? Input.modsMatch(event.modifiers(), MOD_SUPER) + : Input.modsMatch(event.modifiers(), MOD_CONTROL); + + if (ctrl && event.key() == KEY_9) { debug = !debug; return true; } - if ((input.key() == KEY_RETURN || input.key() == KEY_NUMPADENTER) && enterAction != null) { + if ((event.key() == KEY_RETURN || event.key() == KEY_NUMPADENTER) && enterAction != null) { enterAction.run(); return true; } - return super.keyReleased(input); + return super.keyReleased(event); } @Override @@ -239,10 +243,12 @@ public boolean keyPressed(@NonNull KeyEvent input) { return true; } - boolean control = SystemUtils.IS_OS_MAC ? input.modifiers() == MOD_SUPER : input.modifiers() == MOD_CONTROL; + boolean ctrl = SystemUtils.IS_OS_MAC + ? Input.modsMatch(input.modifiers(), MOD_SUPER) + : Input.modsMatch(input.modifiers(), MOD_CONTROL); - return (control && input.key() == KEY_C && toClipboard()) - || (control && input.key() == KEY_V && fromClipboard()); + return (ctrl && input.key() == KEY_C && toClipboard()) + || (ctrl && input.key() == KEY_V && fromClipboard()); } public void keyRepeated(KeyEvent input) { diff --git a/src/main/java/meteordevelopment/meteorclient/gui/screens/ModulesScreen.java b/src/main/java/meteordevelopment/meteorclient/gui/screens/ModulesScreen.java index 1f405535217..f16d538df18 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/screens/ModulesScreen.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/screens/ModulesScreen.java @@ -20,6 +20,7 @@ import meteordevelopment.meteorclient.systems.modules.Module; import meteordevelopment.meteorclient.systems.modules.Modules; import meteordevelopment.meteorclient.utils.misc.NbtUtils; +import meteordevelopment.meteorclient.utils.misc.input.Input; import meteordevelopment.meteorclient.utils.render.DisplayItemUtils; import net.minecraft.client.input.KeyEvent; import net.minecraft.world.item.Items; @@ -152,12 +153,14 @@ protected WWindow createSearch(WContainer c) { } @Override - public boolean keyPressed(@NonNull KeyEvent value) { + public boolean keyPressed(@NonNull KeyEvent input) { if (locked) return false; - boolean cntrl = SystemUtils.IS_OS_MAC ? value.modifiers() == MOD_SUPER : value.modifiers() == MOD_CONTROL; + boolean ctrl = SystemUtils.IS_OS_MAC + ? Input.modsMatch(input.modifiers(), MOD_SUPER) + : Input.modsMatch(input.modifiers(), MOD_CONTROL); - if (cntrl && value.key() == KEY_F) { + if (ctrl && input.key() == KEY_F) { if (searchWindow != null) searchWindow.setExpanded(true); if (searchTextBox != null) { searchTextBox.setFocused(true); @@ -167,7 +170,7 @@ public boolean keyPressed(@NonNull KeyEvent value) { return true; } - return super.keyPressed(value); + return super.keyPressed(input); } // Favorites diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java index 0f10db85ffb..318c45f8643 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java @@ -13,6 +13,7 @@ import meteordevelopment.meteorclient.gui.utils.CharFilter; import meteordevelopment.meteorclient.gui.widgets.WWidget; import meteordevelopment.meteorclient.gui.widgets.containers.WContainer; +import meteordevelopment.meteorclient.utils.misc.input.Input; import meteordevelopment.meteorclient.utils.render.color.Color; import net.minecraft.client.input.CharacterEvent; import net.minecraft.client.input.KeyEvent; @@ -239,25 +240,31 @@ public boolean onMouseReleased(MouseButtonEvent click) { public boolean onKeyPressed(KeyEvent input) { if (!focused) return false; - boolean control = SystemUtils.IS_OS_MAC ? input.modifiers() == MOD_SUPER : input.modifiers() == MOD_CONTROL; + boolean ctrl = SystemUtils.IS_OS_MAC + ? Input.modsMatch(input.modifiers(), MOD_SUPER) + : Input.modsMatch(input.modifiers(), MOD_CONTROL); - if (control && input.key() == KEY_C) { + boolean ctrlShift = SystemUtils.IS_OS_MAC + ? Input.modsMatch(input.modifiers(), MOD_SUPER, MOD_SHIFT) + : Input.modsMatch(input.modifiers(), MOD_CONTROL, MOD_SHIFT); + + if (ctrl && input.key() == KEY_C) { if (cursor != selectionStart || cursor != selectionEnd) { mc.keyboardHandler.setClipboard(text.substring(selectionStart, selectionEnd)); } return true; - } else if (control && input.key() == KEY_X) { + } else if (ctrl && input.key() == KEY_X) { if (cursor != selectionStart || cursor != selectionEnd) { mc.keyboardHandler.setClipboard(text.substring(selectionStart, selectionEnd)); clearSelection(); } return true; - } else if (control && input.key() == KEY_A) { + } else if (ctrl && input.key() == KEY_A) { cursor = text.length(); selectionStart = 0; selectionEnd = cursor; - } else if (input.modifiers() == ((SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL) | MOD_SHIFT) && input.key() == KEY_A) { + } else if (ctrlShift && input.key() == KEY_A) { resetSelection(); } else if (input.key() == KEY_RETURN || input.key() == KEY_NUMPADENTER) { setFocused(false); @@ -298,12 +305,23 @@ public boolean onKeyPressed(KeyEvent input) { public boolean onKeyRepeated(KeyEvent input) { if (!focused) return false; - boolean control = SystemUtils.IS_OS_MAC ? input.modifiers() == MOD_SUPER : input.modifiers() == MOD_CONTROL; - boolean shift = input.modifiers() == MOD_SHIFT; - boolean controlShift = input.modifiers() == ((SystemUtils.IS_OS_WINDOWS ? MOD_ALT : SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL) | MOD_SHIFT); - boolean altShift = input.modifiers() == ((SystemUtils.IS_OS_WINDOWS ? MOD_CONTROL : MOD_ALT) | MOD_SHIFT); + boolean ctrl = SystemUtils.IS_OS_MAC + ? Input.modsMatch(input.modifiers(), MOD_SUPER) + : Input.modsMatch(input.modifiers(), MOD_CONTROL); + + boolean shift = Input.modsMatch(input.modifiers(), MOD_SHIFT); + + // modifier for jumping to the start/end of the text box, and the one for jumping by word + int lineMod = SystemUtils.IS_OS_WINDOWS ? MOD_ALT : SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL; + int wordMod = SystemUtils.IS_OS_WINDOWS ? MOD_CONTROL : MOD_ALT; + + boolean line = Input.modsMatch(input.modifiers(), lineMod); + boolean word = Input.modsMatch(input.modifiers(), wordMod); + boolean ctrlShift = Input.modsMatch(input.modifiers(), lineMod, MOD_SHIFT); + boolean altShift = Input.modsMatch(input.modifiers(), wordMod, MOD_SHIFT); + boolean noMods = Input.modsMatch(input.modifiers()); - if (control && input.key() == KEY_V) { + if (ctrl && input.key() == KEY_V) { clearSelection(); String preText = text; @@ -331,9 +349,9 @@ public boolean onKeyRepeated(KeyEvent input) { if (cursor > 0 && cursor == selectionStart && cursor == selectionEnd) { String preText = text; - int count = (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_ALT : SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL)) + int count = line ? cursor - : (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_CONTROL : MOD_ALT)) + : word ? countToNextSpace(true) : 1; @@ -352,9 +370,9 @@ public boolean onKeyRepeated(KeyEvent input) { if (cursor < text.length()) { String preText = text; - int count = input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_ALT : SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL) + int count = line ? text.length() - cursor - : (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_CONTROL : MOD_ALT)) + : word ? countToNextSpace(false) : 1; @@ -369,12 +387,12 @@ public boolean onKeyRepeated(KeyEvent input) { } else if (input.key() == KEY_LEFT) { if (cursor > 0) { // sets the cursor to just after the next leftmost space - if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_CONTROL : MOD_ALT)) { + if (word) { cursor -= countToNextSpace(true); resetSelection(); } // sets the cursor to the beginning of the text box - else if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_ALT : SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL)) { + else if (line) { cursor = 0; resetSelection(); } @@ -393,7 +411,7 @@ else if (altShift) { } } // sets the selection to the beginning of the text box - else if (controlShift) { + else if (ctrlShift) { if (cursor == selectionEnd && cursor != selectionStart) { selectionEnd = selectionStart; } @@ -423,7 +441,7 @@ else if (shift) { } cursorChanged(); - } else if (selectionStart != selectionEnd && selectionStart == 0 && input.modifiers() == 0) { + } else if (selectionStart != selectionEnd && selectionStart == 0 && noMods) { cursor = 0; resetSelection(); cursorChanged(); @@ -433,12 +451,12 @@ else if (shift) { } else if (input.key() == KEY_RIGHT) { if (cursor < text.length()) { // sets the cursor to just before the next rightmost space - if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_CONTROL : MOD_ALT)) { + if (word) { cursor += countToNextSpace(false); resetSelection(); } // sets the cursor to the end of the text box - else if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? MOD_ALT : SystemUtils.IS_OS_MAC ? MOD_SUPER : MOD_CONTROL)) { + else if (line) { cursor = text.length(); resetSelection(); } @@ -457,7 +475,7 @@ else if (altShift) { } } // sets the selection to the end of the text box - else if (controlShift) { + else if (ctrlShift) { if (cursor == selectionStart && cursor != selectionEnd) { selectionStart = selectionEnd; } @@ -486,7 +504,7 @@ else if (shift) { } cursorChanged(); - } else if (selectionStart != selectionEnd && selectionEnd == text.length() && input.modifiers() == 0) { + } else if (selectionStart != selectionEnd && selectionEnd == text.length() && noMods) { cursor = text.length(); resetSelection(); cursorChanged(); diff --git a/src/main/java/meteordevelopment/meteorclient/utils/misc/input/Input.java b/src/main/java/meteordevelopment/meteorclient/utils/misc/input/Input.java index 27c4a2b3dd5..524e0c7686f 100644 --- a/src/main/java/meteordevelopment/meteorclient/utils/misc/input/Input.java +++ b/src/main/java/meteordevelopment/meteorclient/utils/misc/input/Input.java @@ -16,6 +16,11 @@ public class Input { private static final boolean[] keys = new boolean[512]; private static final boolean[] buttons = new boolean[16]; + private static final int MOD_KEYS = InputConstants.MOD_SHIFT + | InputConstants.MOD_CONTROL + | InputConstants.MOD_ALT + | InputConstants.MOD_SUPER; + private static CursorType lastCursorStyle = CursorTypes.ARROW; private Input() { @@ -69,4 +74,18 @@ public static int getModifier(int key) { default -> 0; }; } + + public static boolean modsMatch(int currentMods, int... desiredMods) { + int desiredMask = 0; + + for (int mod : desiredMods) { + // a required modifier isn't held + if ((currentMods & mod) == 0) return false; + + desiredMask |= mod; + } + + // nothing extra is held, discounting capslock & numlock + return (currentMods & MOD_KEYS & ~desiredMask) == 0; + } }