Gijs Van Oostrum

Make a mod that uses the shoulderbuttons (Button 4 and 5) of a computer mouse. When Clicking 4, your selector should go 1 step forward in your hotbar, and when you press button 5 your selector should move 1 step back in your hotbar. However, the scrollwheel and the shortcuts on the keyboard still need to be active and also select specific places in your hotbar

Kodari AI
KodariOct 28 at 9:14 pm 1
MouseHotbarSwitcher
A Minecraft Fabric mod that adds mouse button hotbar navigation. Pressing mouse button 4 (forward thumb button) moves the hotbar selector one slot forward, while button 5 (back thumb button) moves it one slot backward. The mod integrates seamlessly with existing hotbar controls, keeping the scroll wheel and number key shortcuts fully functional for direct slot selection. It uses Fabric API's key binding system to register the mouse buttons as custom keybindings, listening for press events and adjusting the player's selected hotbar slot accordingly. The mod works client-side, providing smooth and responsive hotbar switching without interfering with vanilla behavior.
  1. Register mouse buttons 4 and 5 as client key mappings.

  2. Cycle the selected hotbar slot forward/backward without changing vanilla scroll or number-key controls.

  3. Add the Fabric 26.3 project scaffold and client entrypoint.

Created the Fabric 26.3 client mod. Mouse button 4 advances one hotbar slot; button 5 moves back, wrapping at the ends. The scroll wheel and number-key shortcuts remain active.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 112
src/client/java/com/kodari/shoulderbuttons/client/ShoulderButtonsClient.java
package com.kodari.shoulderbuttons.client;

import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;

public final class ShoulderButtonsClient implements ClientModInitializer {
    private static final KeyMapping NEXT_SLOT = KeyMappingHelper.registerKeyMapping(
            new KeyMapping("key.shoulderbuttons.next_slot", InputConstants.Type.MOUSE,
                    InputConstants.MOUSE_BUTTON_4, KeyMapping.Category.MISC));
    private static final KeyMapping PREVIOUS_SLOT = KeyMappingHelper.registerKeyMapping(
            new KeyMapping("key.shoulderbuttons.previous_slot", InputConstants.Type.MOUSE,
                    InputConstants.MOUSE_BUTTON_5, KeyMapping.Category.MISC));

    @Override
    public void onInitializeClient() {
        ClientTickEvents.END_CLIENT_TICK.register(ShoulderButtonsClient::onEndTick);
    }

    private static void onEndTick(Minecraft client) {
        while (NEXT_SLOT.consumeClick()) {
            if (client.player != null) {
                var inventory = client.player.getInventory();
                inventory.setSelectedSlot((inventory.getSelectedSlot() + 1) % 9);
            }
        }

        while (PREVIOUS_SLOT.consumeClick()) {
            if (client.player != null) {
                var inventory = client.player.getInventory();
                inventory.setSelectedSlot((inventory.getSelectedSlot() + 8) % 9);
            }
        }
    }
}