05-03-26

Mouse Button to Keyboard Key Mapper Script

This Lua script for Rebind hardware input remaps mouse buttons to keyboard keys. Customize the mappings for Mouse4, Mouse5, and Mouse3 through the UI. The script uses HID output to send the mapped key

rebindlua scriptmouse buttonkey mapperhardware inputhidscript

Question

Mouse button to keyboard key mapper

Rebind AI

luau
-- rebind: name=Mouse Button to Key Mapper

local cfg = UI.Schema({
    mouse4_key = UI.Keybind("4", { label = "Mouse4 Maps To", group = "Button Mapping" }),
    mouse5_key = UI.Keybind("5", { label = "Mouse5 Maps To", group = "Button Mapping" }),
    mouse3_key = UI.Keybind("Delete", { label = "Mouse3 (Middle) Maps To", group = "Button Mapping" }),
    enabled = UI.Toggle(true, { label = "Enable Mapper" }),
})

function OnDown(key)
    if not cfg.enabled then
        return true
    end

    if key == "Mouse4" then
        HID.Down(cfg.mouse4_key)
        return false
    elseif key == "Mouse5" then
        HID.Down(cfg.mouse5_key)
        return false
    elseif key == "Mouse3" then
        HID.Down(cfg.mouse3_key)
        return false
    end

    return true
end

function OnUp(key)
    if not cfg.enabled then
        return true
    end

    if key == "Mouse4" then
        HID.Up(cfg.mouse4_key)
        return false
    elseif key == "Mouse5" then
        HID.Up(cfg.mouse5_key)
        return false
    elseif key == "Mouse3" then
        HID.Up(cfg.mouse3_key)
        return false
    end

    return true
end

function OnStart()
    Log.Info("Mouse Button Mapper loaded")
    Log.Info("Mouse4 → " .. cfg.mouse4_key)
    Log.Info("Mouse5 → " .. cfg.mouse5_key)
    Log.Info("Mouse3 → " .. cfg.mouse3_key)
end

How It Works

This script remaps mouse side buttons (and middle click) to any keyboard keys you choose:

  • Mouse4 (back button) → defaults to 4 key
  • Mouse5 (forward button) → defaults to 5 key
  • Mouse3 (middle click) → defaults to Delete key

The mapper works by:

  1. Intercepting OnDown and OnUp events for each mouse button
  2. Blocking the original input (return false)
  3. Sending the mapped keyboard key instead via HID.Down() / HID.Up()

The script holds the mapped key for as long as you hold the mouse button, preserving the physical hold duration. You can customize each mapping in the UI without restarting.

Learn more: