05-03-26

Mouse Jitter Filter Lua Script

This Lua script for hardware input reduces mouse jitter and tremor while preserving responsiveness. It uses filtering, smoothing, and acceleration to provide steady aiming. Learn how to tune it for yo

rebindlua scriptmousejitterfiltersmoothinghardware input

Question

Can you provide a Lua script to reduce mouse jitter?

Rebind AI

Here's a Lua script to reduce mouse jitter:

lua
-- rebind: min_sdk=0.1.0 mouse_block=true
-- rebind: name=Mouse Jitter Filter
-- rebind: description=Reduce mouse tremor and jitter while preserving intentional movement

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable Filter" }),
    
    jitter_threshold = UI.Slider(2, {
        min = 1,
        max = 10,
        suffix = "px",
        label = "Jitter Threshold",
        tooltip = "Movements smaller than this are filtered out",
        group = "Filtering",
    }),
    
    smoothing = UI.Slider(40, {
        min = 0,
        max = 90,
        suffix = "%",
        label = "Smoothing",
        tooltip = "Higher = smoother but more latency",
        group = "Filtering",
    }),
    
    acceleration_factor = UI.Slider(1.2, {
        min = 1.0,
        max = 3.0,
        step = 0.1,
        label = "Acceleration Factor",
        tooltip = "Boost large movements to maintain responsiveness",
        group = "Advanced",
    }),
})

local buffer = { x = 0, y = 0 }
local lastMagnitude = 0
local frameCounter = 0

function OnMove(dx, dy)
    if not cfg.enabled then
        return true
    end

    -- calculate movement magnitude
    local magnitude = math.sqrt(dx * dx + dy * dy)

    -- filter out sub-threshold jitter
    if magnitude < cfg.jitter_threshold then
        return false
    end

    -- apply exponential smoothing
    local smoothFactor = cfg.smoothing / 100
    buffer.x = buffer.x * smoothFactor + dx * (1 - smoothFactor)
    buffer.y = buffer.y * smoothFactor + dy * (1 - smoothFactor)

    -- apply acceleration boost for large movements to maintain responsiveness
    local accelFactor = 1.0
    if magnitude > cfg.jitter_threshold * 3 then
        accelFactor = cfg.acceleration_factor
    end

    local outX = buffer.x * accelFactor
    local outY = buffer.y * accelFactor

    -- send smoothed movement
    HID.Move(outX, outY)
    lastMagnitude = magnitude
    frameCounter = frameCounter + 1

    return false
end

function OnBlur()
    -- reset buffers when leaving the active window
    buffer.x = 0
    buffer.y = 0
    lastMagnitude = 0
    frameCounter = 0
end

function OnStop()
    Audio.StopAll()
end

This script filters out small mouse movements (jitter) and smooths larger movements. It also includes an acceleration factor to maintain responsiveness.

Key features:

  • Jitter Threshold: Filters out movements below a specified pixel value.
  • Exponential Smoothing: Smooths movements using a time-weighted average.
  • Acceleration Boost: Applies a boost to large movements to maintain responsiveness.

To use it, configure the jitter_threshold, smoothing, and acceleration_factor parameters to suit your needs.