Repository navigation
Expand file tree
/
Copy pathCleanLockv2.ahk
More file actions
317 lines (281 loc) · 7.99 KB
/
Copy pathCleanLockv2.ahk
File metadata and controls
317 lines (281 loc) · 7.99 KB
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
315
316
317
#Requires AutoHotkey v2.0
#SingleInstance Force
#UseHook
; ============================================================
; CleanLock v2.1
;
; Hold F12 for 5 seconds to unlock everything.
; Works whether keyboard, mouse, or both are locked.
;
; Fixes vs v2.0:
; - No arrow functions touching globals (they silently
; created locals and broke the watchdog / pending timer).
; - Every global is declared inside each function.
; - F12 hotkey is only active while something is locked,
; so F12 behaves normally the rest of the time.
; - Hold UI value is rounded so Progress never gets a float.
; - Watchdog auto-cancels when you unlock by hand.
; ============================================================
; ---------------- Config ----------------
HOLD_KEY := "F12"
HOLD_SECS := 5
HOLD_MS := HOLD_SECS * 1000
PRE_SECS := 3
DEFAULT_WD := 120
; ---------------- State ----------------
kbLocked := false
mouseLocked := false
pendingActive := false
pendingTarget := ""
wdActive := false
holdRunning := false
holdArmed := true
holdStart := 0
kbHooks := []
mouseHooks := []
; ---------------- GUI ----------------
g := Gui("+AlwaysOnTop", "CleanLock v2.1")
g.SetFont("s10")
g.Add("Text",, "Lock devices while cleaning.`nHold " HOLD_KEY " for " HOLD_SECS "s to unlock everything.")
btnKb := g.Add("Button", "w300 h36", "Lock KEYBOARD")
btnMouse := g.Add("Button", "w300 h36", "Lock MOUSE")
btnBoth := g.Add("Button", "w300 h36", "Lock BOTH")
btnCancel := g.Add("Button", "w300 h28 Disabled", "Cancel Pending")
g.Add("Text", "y+12", "Watchdog auto-unlock (sec, 0 = off):")
edW := g.Add("Edit", "w60 Number", String(DEFAULT_WD))
status := g.Add("Text", "w300 h56 y+10", "")
holdBar := g.Add("Progress", "w300 h12 Range0-100", 0)
holdTxt := g.Add("Text", "w300 cBlue", "")
g.Add("Text", "w300 cGray y+8",
"Ctrl+Alt+Del, Win+L and the power button cannot be`n"
. "blocked by any AHK script. The watchdog unlocks`n"
. "automatically if you forget, or if the GUI closes.")
btnKb.OnEvent("Click", ToggleKb)
btnMouse.OnEvent("Click", ToggleMouse)
btnBoth.OnEvent("Click", ToggleBoth)
btnCancel.OnEvent("Click", CancelPending)
g.OnEvent("Close", CloseApp)
g.Show()
; ---------------- Keyboard block list ----------------
; Register every VK 0x08..0xFE as a swallowing no-op, except
; the VK that belongs to HOLD_KEY. That keeps F12 available.
skipVK := GetKeyVK(HOLD_KEY)
Loop 0xFE - 0x08 + 1 {
vk := 0x07 + A_Index
if (vk = skipVK)
continue
name := Format("*vk{:02X}", vk)
try {
Hotkey(name, Noop, "Off")
kbHooks.Push(name)
}
}
; ---------------- Mouse block list ----------------
mouseHooks := ["$*LButton", "$*RButton", "$*MButton",
"$*WheelUp", "$*WheelDown", "$*WheelLeft", "$*WheelRight",
"$*XButton1", "$*XButton2"]
for k in mouseHooks
try Hotkey(k, Noop, "Off")
; ---------------- Hold-to-unlock hotkeys (registered, off) ----------------
try Hotkey("*" HOLD_KEY, HoldDown, "Off")
try Hotkey("*" HOLD_KEY " Up", HoldUp, "Off")
; ---------------- Exit handling ----------------
OnExit(OnExitHandler)
; ---------------- Initial UI ----------------
UpdateStatus()
UpdateHoldHotkeys()
; ============================================================
; Functions
; ============================================================
Noop(*) {
}
ToggleKb(*) {
global kbLocked
if (kbLocked)
SetKb(false)
else
BeginPending("kb")
}
ToggleMouse(*) {
global mouseLocked
if (mouseLocked)
SetMouse(false)
else
BeginPending("mouse")
}
ToggleBoth(*) {
global kbLocked, mouseLocked
if (kbLocked && mouseLocked)
UnlockAll()
else
BeginPending("both")
}
BeginPending(target) {
global pendingActive, pendingTarget, btnCancel, status, PRE_SECS
if (pendingActive)
SetTimer(DoPendingLock, 0)
pendingActive := true
pendingTarget := target
btnCancel.Enabled := true
status.Value := "Locking in " PRE_SECS "s. Click Cancel to abort."
SetTimer(DoPendingLock, -PRE_SECS * 1000)
}
CancelPending(*) {
global pendingActive, btnCancel, status
if (!pendingActive)
return
SetTimer(DoPendingLock, 0)
pendingActive := false
btnCancel.Enabled := false
status.Value := "Cancelled."
}
DoPendingLock() {
global pendingActive, pendingTarget, btnCancel
pendingActive := false
btnCancel.Enabled := false
if (pendingTarget = "kb")
SetKb(true)
else if (pendingTarget = "mouse")
SetMouse(true)
else if (pendingTarget = "both") {
SetKb(true)
SetMouse(true)
}
ArmWatchdog()
}
ArmWatchdog() {
global wdActive, edW
if (wdActive) {
SetTimer(DoWatchdog, 0)
wdActive := false
}
sec := 0
try sec := Integer(Trim(edW.Value))
if (sec > 0) {
wdActive := true
SetTimer(DoWatchdog, -sec * 1000)
}
}
DoWatchdog() {
global wdActive
wdActive := false
UnlockAll()
}
SetKb(state) {
global kbLocked, kbHooks
kbLocked := state
for k in kbHooks
try Hotkey(k, state ? "On" : "Off")
UpdateStatus()
UpdateHoldHotkeys()
CheckWatchdogState()
}
SetMouse(state) {
global mouseLocked, mouseHooks
mouseLocked := state
for k in mouseHooks
try Hotkey(k, state ? "On" : "Off")
try {
if (state)
BlockInput("MouseMove")
else
BlockInput("MouseMoveOff")
}
UpdateStatus()
UpdateHoldHotkeys()
CheckWatchdogState()
}
UnlockAll(*) {
global pendingActive, btnCancel
if (pendingActive) {
SetTimer(DoPendingLock, 0)
pendingActive := false
btnCancel.Enabled := false
}
SetKb(false)
SetMouse(false)
ResetHoldUI()
}
CheckWatchdogState() {
global kbLocked, mouseLocked, wdActive
if (!(kbLocked || mouseLocked) && wdActive) {
SetTimer(DoWatchdog, 0)
wdActive := false
}
}
UpdateHoldHotkeys() {
global kbLocked, mouseLocked, HOLD_KEY
if (kbLocked || mouseLocked) {
try Hotkey("*" HOLD_KEY, HoldDown, "On")
try Hotkey("*" HOLD_KEY " Up", HoldUp, "On")
} else {
try Hotkey("*" HOLD_KEY, "Off")
try Hotkey("*" HOLD_KEY " Up", "Off")
}
}
HoldDown(*) {
global kbLocked, mouseLocked, holdRunning, holdArmed, holdStart
if (!(kbLocked || mouseLocked))
return
if (!holdArmed)
return
holdArmed := false
holdRunning := true
holdStart := A_TickCount
SetTimer(TickHold, 50)
}
HoldUp(*) {
global holdRunning, holdArmed
holdArmed := true
if (!holdRunning)
return
holdRunning := false
SetTimer(TickHold, 0)
ResetHoldUI()
}
TickHold() {
global holdRunning, holdStart, holdBar, holdTxt, HOLD_SECS, HOLD_MS
if (!holdRunning)
return
el := A_TickCount - holdStart
if (el >= HOLD_MS) {
holdRunning := false
SetTimer(TickHold, 0)
holdBar.Value := 100
holdTxt.Value := "Unlocking..."
SetTimer(UnlockAll, -150)
return
}
holdBar.Value := Round(el / HOLD_MS * 100)
holdTxt.Value := Format("Hold: {:.1f}s / {}s", el / 1000, HOLD_SECS)
}
ResetHoldUI() {
global holdBar, holdTxt
holdBar.Value := 0
holdTxt.Value := ""
}
UpdateStatus() {
global kbLocked, mouseLocked, status, btnKb, btnMouse, btnBoth
kt := kbLocked ? "LOCKED" : "free"
mt := mouseLocked ? "LOCKED" : "free"
status.Value := Format("Keyboard: {1}`nMouse: {2}", kt, mt)
btnKb.Text := kbLocked ? "Unlock Keyboard" : "Lock KEYBOARD"
btnMouse.Text := mouseLocked ? "Unlock Mouse" : "Lock MOUSE"
btnBoth.Text := (kbLocked && mouseLocked) ? "Unlock BOTH" : "Lock BOTH"
}
CloseApp(*) {
ExitApp()
}
OnExitHandler(*) {
global kbHooks, mouseHooks
try SetTimer(DoPendingLock, 0)
try SetTimer(DoWatchdog, 0)
try SetTimer(TickHold, 0)
for k in kbHooks
try Hotkey(k, "Off")
for k in mouseHooks
try Hotkey(k, "Off")
try BlockInput("MouseMoveOff")
try BlockInput("Off")
return 0
}