05-03-26

Mouse DPI Changer Lua Script

This Lua script for Rebind hardware input allows you to cycle through DPI presets and adjust mouse sensitivity. Customize DPI values and hotkeys for a personalized gaming experience.

rebindlua scriptdpimousesensitivityinputscript

Question

I need a simple DPI changer for my mouse. Use logical intervals.

Rebind AI

lua
-- rebind: name=DPI Changer
-- rebind: description=Cycle through DPI presets with a hotkey

local cfg = UI.Schema({
    toggle_key = UI.Keybind("F7", { label = "Cycle DPI Key" }),
    dpi_presets = UI.Text("400, 800, 1600, 3200", {
        label = "DPI Presets",
        tooltip = "Comma-separated values",
    }),
})

local dpiList = {}
local currentIndex = 1

function OnStart()
    -- Parse the DPI presets from the config string
    local presetStr = cfg.dpi_presets
    for dpiStr in presetStr:gmatch("[^,]+") do
        local dpi = tonumber(dpiStr:match("%d+"))
        if dpi then
            table.insert(dpiList, dpi)
        end
    end

    if #dpiList == 0 then
        table.insert(dpiList, 800)
        table.insert(dpiList, 1600)
        table.insert(dpiList, 3200)
    end

    Log.Info("DPI Presets: " .. table.concat(dpiList, ", "))
end

Bind(cfg.toggle_key, function()
    if #dpiList == 0 then
        UI.Notify("No DPI presets configured", "error")
        return
    end

    currentIndex = currentIndex % #dpiList + 1
    local dpi = dpiList[currentIndex]

    UI.Notify("DPI: " .. dpi, "info")
    Log.Info("Switched to DPI " .. dpi)

    return false
end)

How it works:

  1. Modular DPI list — Presets are user-configurable in the UI text field ("400, 800, 1600, 3200")
  2. Logical intervals — Default progression doubles each step (400 → 800 → 1600 → 3200), which is the standard gaming DPI scale
  3. Cycle through — Each press of F7 moves to the next DPI in the list, wrapping around
  4. Notification — Shows the current DPI on screen and logs it
  5. Bind for simplicity — Uses Bind instead of OnDown, so the hotkey is blocked automatically

To customize:

  • Edit the dpi_presets field in the UI to add/remove DPI values
  • Change the toggle_key to whatever hotkey you prefer
  • The script automatically parses and validates the comma-separated list

Learn more:

Question

Yes but it isn't changing the mouse movement speed..

Rebind AI

You're right — the script was just displaying the DPI, not actually changing mouse sensitivity. Here's the corrected version that scales mouse movement:

lua
-- rebind: name=DPI Changer
-- rebind: description=Cycle through DPI presets and adjust mouse speed

local cfg = UI.Schema({
    toggle_key = UI.Keybind("F7", { label = "Cycle DPI Key" }),
    dpi_presets = UI.Text("400, 800, 1600, 3200", {
        label = "DPI Presets",
        tooltip = "Comma-separated values",
    }),
    base_dpi = UI.Slider(800, {
        min = 100,
        max = 5000,
        label = "Reference DPI",
        tooltip = "The DPI value that represents 1.0x sensitivity",
    }),
})

local dpiList = {}
local currentIndex = 1
local currentDpi = 800

function OnStart()
    -- Parse the DPI presets from the config string
    local presetStr = cfg.dpi_presets
    for dpiStr in presetStr:gmatch("[^,]+") do
        local dpi = tonumber(dpiStr:match("%d+"))
        if dpi then
            table.insert(dpiList, dpi)
        end
    end

    if #dpiList == 0 then
        table.insert(dpiList, 800)
        table.insert(dpiList, 1600)
        table.insert(dpiList, 3200)
    end

    currentDpi = dpiList[1]
    Log.Info("DPI Presets: " .. table.concat(dpiList, ", "))
end

Bind(cfg.toggle_key, function()
    if #dpiList == 0 then
        UI.Notify("No DPI presets configured", "error")
        return false
    end

    currentIndex = currentIndex % #dpiList + 1
    currentDpi = dpiList[currentIndex]

    UI.Notify("DPI: " .. currentDpi, "info")
    Log.Info("Switched to DPI " .. currentDpi)

    return false
end)

function OnMove(dx, dy)
    -- Scale mouse movement based on current DPI vs base DPI
    local multiplier = currentDpi / cfg.base_dpi
    HID.Move(dx * multiplier, dy * multiplier)
    return false
end

What changed:

  1. OnMove handler — Intercepts every mouse movement and scales it by (currentDpi / baseDpi)
  2. Base DPI reference — Added base_dpi slider to set what 1.0x sensitivity means (typically 800)
  3. Returns false — Blocks the original movement and sends the scaled version instead

How it works:

  • Lower DPI (400) → multiplier = 0.5x → slower movement
  • Higher DPI (3200) → multiplier = 4.0x → faster movement
  • Switching DPI mid-game instantly changes your sensitivity

Learn more:

Question

Thank you, much better.

Rebind AI

You're welcome! Glad it's working now. If you need any tweaks — like adjusting the DPI intervals, adding acceleration, or changing the hotkey — just let me know.