1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
local GAMES = require("lib/games")
local cfg = UI.Schema({
enabled = UI.Toggle(true, { label = "Enable Snapmove" }),
toggle_key = UI.Keybind("F11", { label = "Toggle Key" }),
auto_detect = UI.Toggle(true, {
label = "Auto-detect games",
group = "Game Detection",
tooltip = "Active in known FPS games (CS2, VALORANT, Apex, CoD, etc.)",
}),
targets = UI.Text("", {
label = "Window/Process Targets",
group = "Game Detection",
placeholder = "comma-separated, e.g. cs2, valorant, r5apex",
tooltip = "Optional extra targets. Leave empty if auto-detect covers your games. Matches against window title and process name (case-insensitive).",
}),
humanize = UI.Toggle(true, {
label = "Humanize overlap",
group = "Behavior",
tooltip = "Randomize each overlap duration between the min and max for natural-feeling timing",
}),
overlap_min = UI.Slider(15, {
min = 0,
max = 55,
suffix = "ms",
group = "Behavior",
label = "Overlap Min",
showIf = "humanize",
tooltip = "Minimum overlap when humanize is on",
}),
overlap_max = UI.Slider(25, {
min = 0,
max = 60,
suffix = "ms",
group = "Behavior",
label = "Overlap Max",
tooltip = "Maximum time both opposing keys stay held together before the loser releases",
}),
debug_global = UI.Toggle(false, {
label = "Debug: run globally",
group = "Diagnostics",
tooltip = "Ignore game detection and run snapmove in every application",
}),
debug_log = UI.Toggle(false, {
label = "Debug logging",
group = "Diagnostics",
tooltip = "Log every key state change to the Rebind console",
}),
})
local manualList = {}
local manualStr = ""
local function matchTarget()
if cfg.debug_global then
return true
end
if cfg.auto_detect then
local win = System.Window()
local title = win.title:lower()
local proc = win.process:lower()
for _, t in ipairs(GAMES) do
if title:find(t, 1, true) or proc:find(t, 1, true) then
return true
end
end
end
local raw = cfg.targets
if raw == "" then
return not cfg.auto_detect
end
if raw ~= manualStr then
manualList = {}
for part in raw:gmatch("[^,]+") do
local t = part:match("^%s*(.-)%s*$")
if #t > 0 then
manualList[#manualList + 1] = t:lower()
end
end
manualStr = raw
end
if #manualList == 0 then
return not cfg.auto_detect
end
local win = System.Window()
local title = win.title:lower()
local proc = win.process:lower()
for _, t in ipairs(manualList) do
if title:find(t, 1, true) or proc:find(t, 1, true) then
return true
end
end
return false
end
local KEYS = { "W", "A", "S", "D" }
local OPPOSITE = { W = "S", S = "W", A = "D", D = "A" }
local AXIS_NAME =
{ A = "horizontal", D = "horizontal", W = "vertical", S = "vertical" }
local held = {}
local virt = {}
local lastH = nil
local lastV = nil
local timers = {}
local function dbg(msg)
if cfg.debug_log then
Log.Info("[snapmove] " .. msg)
end
end
local function setVirt(key, down)
if virt[key] == down then
return
end
if down then
HID.Down(key)
dbg("HID.Down " .. key)
else
HID.Up(key)
dbg("HID.Up " .. key)
end
virt[key] = down
end
local function resolveAxis(a, b, lastPressed)
for _, k in ipairs({ a, b }) do
if timers[k] then
timers[k]:Cancel()
timers[k] = nil
end
end
local aHeld = held[a]
local bHeld = held[b]
if aHeld and bHeld then
local winner = lastPressed or a
local loser = (winner == a) and b or a
setVirt(winner, true)
setVirt(loser, true)
local overlapMs
if cfg.humanize then
overlapMs = Math.Random(cfg.overlap_min, cfg.overlap_max)
else
overlapMs = cfg.overlap_max
end
dbg(
"SOCD "
.. AXIS_NAME[winner]
.. " winner="
.. winner
.. " loser="
.. loser
.. " overlap="
.. overlapMs
.. "ms"
)
if overlapMs > 0 then
timers[a] = Timer.After(overlapMs, function()
if held[winner] and held[loser] then
setVirt(loser, false)
dbg("SOCD " .. AXIS_NAME[winner] .. " overlap end released " .. loser)
end
end)
else
setVirt(loser, false)
end
else
setVirt(a, aHeld or false)
setVirt(b, bHeld or false)
end
end
local function resolveAll()
resolveAxis("A", "D", lastH)
resolveAxis("W", "S", lastV)
end
local function releaseAll()
for _, t in pairs(timers) do
if t then
t:Cancel()
end
end
timers = {}
for _, key in ipairs(KEYS) do
setVirt(key, false)
end
held = {}
lastH, lastV = nil, nil
dbg("all keys released")
end
function OnStop()
releaseAll()
end
function OnBlur()
releaseAll()
end
function OnTick()
if not cfg.enabled then
return
end
if not matchTarget() then
return
end
local dirty = false
for _, key in ipairs(KEYS) do
if held[key] and not Input.IsDown(key) then
held[key] = nil
dbg("OnTick caught release of " .. key)
dirty = true
end
end
if dirty then
resolveAll()
end
end
function OnDown(key)
if key == cfg.toggle_key then
cfg.enabled = not cfg.enabled
if not cfg.enabled then
releaseAll()
end
UI.Notify(cfg.enabled and "Snapmove ON" or "Snapmove OFF", "info")
dbg("toggled " .. (cfg.enabled and "ON" or "OFF"))
return false
end
if not cfg.enabled then
return true
end
if not matchTarget() then
return true
end
if not OPPOSITE[key] then
return true
end
held[key] = true
if key == "A" or key == "D" then
lastH = key
else
lastV = key
end
dbg("press " .. key .. " held=" .. #Input.GetActiveKeys())
resolveAll()
return false
end
function OnUp(key)
if not cfg.enabled then
return true
end
if not matchTarget() then
return true
end
if not OPPOSITE[key] then
return true
end
held[key] = nil
dbg("release " .. key)
resolveAll()
return false
end