From d6ac3d4d2bf572910652dafc249b513ce7ce3591 Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 22 Jul 2026 09:26:28 +0200 Subject: [PATCH] Add 'Lock Scroll While Attacking' option Adds an opt-in option (default off) to the Mouse Settings screen that ignores scroll events while the attack key is held down and the player is in game (no screen or overlay open). This prevents a small finger movement on a Magic Mouse / trackpad (or any mouse) from producing an accidental horizontal scroll that switches the selected hotbar item while mining or attacking. - The option is added for all platforms and persisted in the mod options file. - The scroll is blocked in MouseHandlerMixin (loaded on every version) at the head of onScroll, so it applies regardless of the downstream hotbar-scroll path (Inventory.scrollInHotbar vs newer). - The trigger uses options.keyAttack.isDown(), so it follows any rebind of the attack key rather than a hard-coded left mouse button. --- .../macos_input_fixes/client/ModOptions.java | 40 +++++++++++++++++-- .../client/mixin/MouseHandlerMixin.java | 34 ++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/client/java/com/hamarb123/macos_input_fixes/client/ModOptions.java b/src/client/java/com/hamarb123/macos_input_fixes/client/ModOptions.java index cc245ed..bd89cf4 100644 --- a/src/client/java/com/hamarb123/macos_input_fixes/client/ModOptions.java +++ b/src/client/java/com/hamarb123/macos_input_fixes/client/ModOptions.java @@ -513,22 +513,24 @@ public static Object[] getModOptions() loadInterface(); //load the elements if they are not loaded yet if (Common.IS_SYSTEM_MAC) { - //on macOS show reverse scrolling, reverse hotbar scrolling, trackpad sensitivity, momentum scrolling, interface smooth scroll options, disable ctrl+click fix - Object[] arr = new Object[6]; + //on macOS show reverse scrolling, reverse hotbar scrolling, trackpad sensitivity, momentum scrolling, interface smooth scroll options, disable ctrl+click fix, lock scroll while attacking + Object[] arr = new Object[7]; arr[0] = REVERSE_SCROLLING; arr[1] = REVERSE_HOTBAR_SCROLLING; arr[2] = TRACKPAD_SENSITIVITY; arr[3] = MOMENTUM_SCROLLING; arr[4] = INTERFACE_SMOOTH_SCROLL; arr[5] = DISABLE_CTRL_CLICK_FIX; + arr[6] = LOCK_SCROLL_WHILE_ATTACKING; return arr; } else { - //otherwise show reverse scrolling, and reverse hotbar scrolling options only - Object[] arr = new Object[2]; + //otherwise show reverse scrolling, reverse hotbar scrolling, and lock scroll while attacking options only + Object[] arr = new Object[3]; arr[0] = REVERSE_SCROLLING; arr[1] = REVERSE_HOTBAR_SCROLLING; + arr[2] = LOCK_SCROLL_WHILE_ATTACKING; return arr; } } @@ -580,6 +582,13 @@ private static void loadInterface() (value) -> reverseHotbarScrolling = value, "Reverses the direction that scrolling goes for the hotbar when enabled."); + LOCK_SCROLL_WHILE_ATTACKING = booleanOption( + "options.macos_input_fixes.lock_scroll_while_attacking", + "Lock Scroll While Attacking", + () -> lockScrollWhileAttacking, + (value) -> lockScrollWhileAttacking = value, + "Ignores scrolling while the attack key is held down and you are in game (no screen open).\nUseful on a Magic Mouse or trackpad, where a small finger movement while mining\ncauses an accidental scroll that switches the selected hotbar item.\nDefault: OFF\nOFF: scrolling always works.\nON: scrolling is ignored while the attack key is held down."); + REVERSE_SCROLLING = booleanOption( "options.macos_input_fixes.reverse_scrolling", "Reverse Scrolling", @@ -748,6 +757,20 @@ public static void loadOptions() } disableCtrlClickFix = actualValue; } + if (compoundTag.contains("lockScrollWhileAttacking")) //read lockScrollWhileAttacking option + { + boolean actualValue = false; //default value + try + { + Boolean value = Boolean.parseBoolean(getStringHelper(compoundTag, "lockScrollWhileAttacking")); + actualValue = value; + } + catch (Exception ex1) + { + ex1.printStackTrace(System.err); //failed to parse + } + lockScrollWhileAttacking = actualValue; + } loadedInterface = false; } @@ -768,6 +791,7 @@ public static void saveOptions() printWriter.println("momentumScrolling:" + momentumScrolling); printWriter.println("interfaceSmoothScroll:" + interfaceSmoothScroll); printWriter.println("disableCtrlClickFix:" + disableCtrlClickFix); + printWriter.println("lockScrollWhileAttacking:" + lockScrollWhileAttacking); } catch (Exception ex2) { @@ -828,4 +852,12 @@ public static void setInterfaceSmoothScroll(boolean value) public static boolean disableCtrlClickFix = false; public static Object DISABLE_CTRL_CLICK_FIX; + + //lock scroll while attacking option code: + //when enabled, scroll events are ignored while the attack key is held down and we're in game (no screen open) + //this stops a small finger movement on a Magic Mouse / trackpad from changing the selected hotbar slot while mining + //this option works on all platforms + + public static boolean lockScrollWhileAttacking = false; + public static Object LOCK_SCROLL_WHILE_ATTACKING; } diff --git a/src/client/java/com/hamarb123/macos_input_fixes/client/mixin/MouseHandlerMixin.java b/src/client/java/com/hamarb123/macos_input_fixes/client/mixin/MouseHandlerMixin.java index 1d2d7ec..f26b6ab 100644 --- a/src/client/java/com/hamarb123/macos_input_fixes/client/mixin/MouseHandlerMixin.java +++ b/src/client/java/com/hamarb123/macos_input_fixes/client/mixin/MouseHandlerMixin.java @@ -1,7 +1,9 @@ package com.hamarb123.macos_input_fixes.client.mixin; +import net.minecraft.client.Minecraft; import net.minecraft.client.MouseHandler; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; @@ -10,9 +12,41 @@ import com.hamarb123.macos_input_fixes.client.Common; import com.hamarb123.macos_input_fixes.client.ModOptions; +//? if >=26.1 { +import com.hamarb123.macos_input_fixes.client.ModernFabricReflectionHelper; +//?} + @Mixin(MouseHandler.class) public class MouseHandlerMixin { + @Shadow + private Minecraft minecraft; + + //if the lock scroll while attacking option is enabled, ignore scroll events while the attack key is held down in game + //this stops a small finger movement on a Magic Mouse / trackpad from changing the selected hotbar slot while mining + //this works on all platforms + @Inject(at = @At("HEAD"), method = "onScroll(JDD)V", cancellable = true) + private void lockScrollWhileAttacking(long window, double horizontal, double vertical, CallbackInfo info) + { + //only act if the option is on and we're actually in game (no screen or overlay open) + if (!ModOptions.lockScrollWhileAttacking) return; + //? if >=26.1 { + if (ModernFabricReflectionHelper.getOverlay(this.minecraft) != null) return; + if (ModernFabricReflectionHelper.getScreen(this.minecraft) != null) return; + //?} else { + /* + if (this.minecraft.getOverlay() != null) return; + if (this.minecraft.screen != null) return; + *///?} + if (this.minecraft.player == null) return; + + //cancel the scroll while the attack key is held down (default left click, follows any rebind of the attack key) + if (this.minecraft.options.keyAttack.isDown()) + { + info.cancel(); + } + } + @Inject(at = @At("HEAD"), method = "onScroll(JDD)V", cancellable = true) private void onMouseScroll(long window, double horizontal, double vertical, CallbackInfo info) {