05-07-26
SOCD Cleaner with Humanized Overlap
This Lua script for Rebind hardware input implements SOCD cleaning with humanized overlap. It resolves simultaneous opposite cardinal directions (A+D, W+S) using last-key-wins logic and a randomized d
Question
Can you provide a Rebind Lua script for SOCD cleaning with humanized overlap?
Rebind AI
-- rebind: min_sdk=0.1.0
-- rebind: tick_rate=8000
-- rebind: name=SOCD Cleaner
local cfg = UI.Schema({
enabled = UI.Toggle(true, { label = "Enable SOCD" }),
overlap_min = UI.Slider(20, { min = 0, max = 60, label = "Overlap Min", suffix = "ms", group = "Timing" }),
overlap_max = UI.Slider(35, { min = 0, max = 100, label = "Overlap Max", suffix = "ms", group = "Timing" }),
})
-- Map opposite keys
local OPPOSITES = {
A = "D", D = "A",
W = "S", S = "W",
}
local held = {} -- physical keys currently held
local virt = {} -- virtual keys currently active (sent to HID)
local lastH = nil -- last horizontal key pressed (for LKW resolution)
local lastV = nil -- last vertical key pressed (for LKW resolution)
local function sendVirtual(key, on)
if virt[key] == on then return end
if on then
HID.Down(key)
else
HID.Up(key)
end
virt[key] = on
end
local function resolve(key1, key2, lastKey)
-- Both keys held: send last-key-wins with humanized delay
if held[key1] and held[key2] then
sendVirtual(lastKey, true)
local delay = Math.Random(cfg.overlap_min, math.max(cfg.overlap_min, cfg.overlap_max))
if delay > 0 then
Timer.After(delay, function()
-- Check both still held (may have been released during delay)
if held[key1] and held[key2] then
local otherKey = lastKey == key1 and key2 or key1
sendVirtual(otherKey, false)
end
end)
else
local otherKey = lastKey == key1 and key2 or key1
sendVirtual(otherKey, false)
end
else
-- Only one held: send it, release the other
sendVirtual(key1, held[key1] or false)
sendVirtual(key2, held[key2] or false)
end
end
function OnDown(key)
if not cfg.enabled or not OPPOSITES[key] then
return true
end
held[key] = true
-- Track last key pressed for each axis
if key == "A" or key == "D" then
lastH = key
end
if key == "W" or key == "S" then
lastV = key
end
-- Resolve both axes independently
resolve("A", "D", lastH)
resolve("W", "S", lastV)
return false
end
function OnUp(key)
if not cfg.enabled or not OPPOSITES[key] then
return true
end
held[key] = false
-- Resolve both axes
resolve("A", "D", lastH)
resolve("W", "S", lastV)
return false
end
function OnBlur()
-- Release all virtual keys and reset state
for key, _ in pairs(virt) do
sendVirtual(key, false)
end
held = {}
virt = {}
lastH = nil
lastV = nil
endHow It Works
SOCD (Simultaneous Opposite Cardinal Directions) is when a player presses conflicting keys at the same time (A+D, W+S). This script implements Last-Key-Wins resolution: whichever key was pressed most recently wins, and the other is suppressed.
Humanized Overlap adds a variable delay (randomized between overlap_min and overlap_max milliseconds) before suppressing the losing key. This makes the behavior feel more natural and less mechanical than instant suppression.
Key Features
- Independent axes: Horizontal (A/D) and vertical (W/S) are resolved separately, so you can hold A+S simultaneously
- Last-key-wins: Track
lastHandlastVto know which key was pressed most recently on each axis - Humanized timing: Use
Math.Random()to vary the suppression delay for each SOCD event - Blocks conflicting input: Returns
falsefromOnDown/OnUpto prevent the conflict from reaching the game - High tick rate:
tick_rate=8000ensures timers fire with 0.125ms precision, crucial for fighting games - Cleanup on blur: Release all held virtual keys when the game loses focus
Usage
Enable in the UI, then hold conflicting keys (e.g., A+D) to see the last one you pressed "win" after a brief, variable delay.
Learn more: