05-07-26

Mouse DPI and Button Remapping Lua Script

This Lua script for hardware input allows you to remap mouse buttons and cycle through DPI settings. Customize DPI presets and keybinds via the UI for a personalized experience.

rebindlua scriptmousedpiremapbuttonscript

Question

Can you provide a Lua script to change DPI and remap mouse buttons?

Rebind AI

Here's a Lua script that allows you to change DPI and remap mouse buttons:

lua
-- rebind: min_sdk=0.1.0
-- rebind: name=Mouse DPI & Button Remapper

local cfg = UI.Schema({
    -- DPI Settings
    dpi_low = UI.Slider(400, {
        min = 100,
        max = 3200,
        step = 50,
        label = "Low DPI",
        group = "DPI Presets",
    }),
    dpi_med = UI.Slider(800, {
        min = 100,
        max = 3200,
        step = 50,
        label = "Medium DPI",
        group = "DPI Presets",
    }),
    dpi_high = UI.Slider(1600, {
        min = 100,
        max = 3200,
        step = 50,
        label = "High DPI",
        group = "DPI Presets",
    }),
    dpi_cycle_key = UI.Keybind("F12", {
        label = "Cycle DPI Key",
        group = "DPI Controls",
    }),
    dpi_low_key = UI.Keybind("F9", {
        label = "Low DPI Key",
        group = "DPI Controls",
    }),
    dpi_med_key = UI.Keybind("F10", {
        label = "Medium DPI Key",
        group = "DPI Controls",
    }),
    dpi_high_key = UI.Keybind("F11", {
        label = "High DPI Key",
        group = "DPI Controls",
    }),

    -- Button Remapping
    remap_m4 = UI.Keybind("4", {
        label = "Mouse4 → Key",
        group = "Button Remapping",
    }),
    remap_m5 = UI.Keybind("5", {
        label = "Mouse5 → Key",
        group = "Button Remapping",
    }),
})

local currentDpi = cfg.dpi_med
local dpiLevels = {}

function OnStart()
    dpiLevels = { cfg.dpi_low, cfg.dpi_med, cfg.dpi_high }
    currentDpi = cfg.dpi_med
    UI.Notify("Mouse Manager loaded. Current DPI: " .. currentDpi, "info")
end

-- Mouse sensitivity scaling (simulated via movement filtering)
local function scaleMouse(dx, dy, targetDpi)
    local baseDpi = 800
    local factor = targetDpi / baseDpi
    return dx * factor, dy * factor
end

-- DPI cycling
local dpiIndex = 2

function OnDown(key)
    -- Cycle through DPI levels
    if key == cfg.dpi_cycle_key then
        dpiIndex = dpiIndex + 1
        if dpiIndex > #dpiLevels then
            dpiIndex = 1
        end
        currentDpi = dpiLevels[dpiIndex]
        UI.Notify("DPI: " .. currentDpi, "info")
        return false
    end

    -- Direct DPI presets
    if key == cfg.dpi_low_key then
        currentDpi = cfg.dpi_low
        dpiIndex = 1
        UI.Notify("DPI: " .. currentDpi .. " (Low)", "info")
        return false
    end

    if key == cfg.dpi_med_key then
        currentDpi = cfg.dpi_med
        dpiIndex = 2
        UI.Notify("DPI: " .. currentDpi .. " (Medium)", "info")
        return false
    end

    if key == cfg.dpi_high_key then
        currentDpi = cfg.dpi_high
        dpiIndex = 3
        UI.Notify("DPI: " .. currentDpi .. " (High)", "info")
        return false
    end

    -- Button remapping
    if key == "Mouse4" then
        HID.Press(cfg.remap_m4)
        return false
    end

    if key == "Mouse5" then
        HID.Press(cfg.remap_m5)
        return false
    end

    return true
end

-- Apply DPI scaling to mouse movement
function OnMove(dx, dy)
    local scaledX, scaledY = scaleMouse(dx, dy, currentDpi)
    HID.Move(scaledX, scaledY)
    return false
end

function OnUp(key)
    return true
end

function OnBlur()
    currentDpi = cfg.dpi_med
    dpiIndex = 2
end

This script provides DPI cycling and mouse button remapping.

DPI Features:

  • Three presets: Configure Low/Medium/High DPI values (100-3200 range, 50-point increments)
  • Cycle key: Press F12 to rotate through all three DPI levels
  • Direct presets: F9/F10/F11 jump to a specific DPI instantly
  • Movement scaling: Mouse sensitivity scales proportionally to the DPI setting
  • Notifications: See the current DPI whenever it changes

Button Remapping:

  • Mouse4 & Mouse5: Remap to any key you want (default: 4 and 5)
  • Press-triggered: Fires on button down, blocks the original input
  • Customize both buttons independently via the UI

All values are saved automatically and restore when the script reloads. Adjust the UI sliders or hotkeys without editing code.