05-03-26

Smart Crouch Toggle with Movement Check

This Lua script for hardware input allows you to toggle crouch only when moving, and automatically releases crouch when you stop. It's useful in games to avoid getting stuck crouched. Configure keys a

rebindlua scriptcrouchtogglemovementhardware inputscript

Question

Can you provide a Lua script for a smart crouch toggle with movement detection?

Rebind AI

Here's a Lua script for a smart crouch toggle:

lua
-- rebind: min_sdk=0.1.0
-- rebind: name=Smart Crouch Toggle
-- rebind: description=Toggle crouch only when moving; auto-release when stopped

local cfg = UI.Schema({
    crouch_key = UI.Keybind("LCtrl", { label = "Crouch Key" }),
    movement_keys = UI.Text("W,A,S,D", { label = "Movement Keys (comma-separated)", maxLength = 50 }),
    auto_release = UI.Toggle(true, { label = "Auto-Release When Still" }),
    stillness_time = UI.Slider(200, { min = 50, max = 1000, suffix = "ms", showIf = "auto_release" }),
})

local crouched = false
local lastMovementTime = 0
local movementSet = {}

function OnStart()
    -- Parse movement keys from config
    for key in cfg.movement_keys:gmatch("([^,]+)") do
        movementSet[key:match("^%s*(.-)%s*$")] = true
    end
    Log.Info("Smart Crouch loaded. Movement keys: " .. cfg.movement_keys)
end

function OnDown(key)
    if key == cfg.crouch_key then
        -- Only allow crouch toggle if moving or auto_release is disabled
        if movementSet[key] or not cfg.auto_release or (System.Time() - lastMovementTime < cfg.stillness_time) then
            crouched = not crouched
            if crouched then
                HID.Down(cfg.crouch_key)
                UI.Notify("Crouch: ON", "info")
            else
                HID.Up(cfg.crouch_key)
                UI.Notify("Crouch: OFF", "info")
            end
        end
        return false
    end

    -- Track movement key presses
    if movementSet[key] then
        lastMovementTime = System.Time()
    end

    return true
end

function OnUp(key)
    if key == cfg.crouch_key then
        return false
    end

    -- If we released a movement key and stillness timer expired, auto-release crouch
    if cfg.auto_release and crouched and movementSet[key] then
        Run(function()
            Sleep(cfg.stillness_time)
            if crouched and not Input.IsDown("W") and not Input.IsDown("A") and not Input.IsDown("S") and not Input.IsDown("D") then
                crouched = false
                HID.Up(cfg.crouch_key)
                UI.Notify("Crouch: AUTO-RELEASED", "info")
            end
        end)
    end

    return true
end

function OnBlur()
    if crouched then
        crouched = false
        HID.Up(cfg.crouch_key)
    end
end

function OnStop()
    if crouched then
        crouched = false
        HID.Up(cfg.crouch_key)
    end
end

This script provides the following features:

  • Movement Detection: Detects when movement keys are pressed.
  • Toggle on Movement: Allows crouching only when a movement key is held.
  • Auto-Release: Automatically uncrouches when movement stops after a configurable delay.
  • Configuration: Customizable crouch key, movement keys, auto-release, and stillness time.
  • State Cleanup: Ensures crouch is released when the script loses focus or stops.
  • Feedback: Provides UI notifications for state changes.

Configuration Options:

  • crouch_key: The key bound to crouch (default: LCtrl).
  • movement_keys: Comma-separated list of keys that enable crouch toggling (default: W,A,S,D).
  • auto_release: If enabled, crouch automatically releases when you stop moving.
  • stillness_time: How long to wait after the last movement input before auto-releasing (200ms default).