Repository navigation
Expand file tree
/
Copy pathCleanLock.ahk
More file actions
115 lines (101 loc) · 3.2 KB
/
Copy pathCleanLock.ahk
File metadata and controls
115 lines (101 loc) · 3.2 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
#Requires AutoHotkey v2.0
#SingleInstance Force
; Run as administrator for best results
kbLocked := false
mouseLocked := false
clickTimes := []
; ---------- GUI ----------
g := Gui("+AlwaysOnTop", "CleanLock")
g.SetFont("s10")
g.Add("Text",, "Lock for cleaning (3 sec delay after pressing):")
btnKb := g.Add("Button", "w220 h35", "Lock KEYBOARD")
btnMouse := g.Add("Button", "w220 h35", "Lock MOUSE")
g.Add("Text", "y+15", "Safety auto-unlock after (sec, 0 = off):")
edSafety := g.Add("Edit", "w60 Number", "120")
status := g.Add("Text", "w260 h45 y+15", "")
helpText := "Unlock macros:`nKeyboard: triple right-click (mouse must be unlocked)`nMouse: Ctrl+Alt+M (keyboard must be unlocked)`nEmergency: Ctrl+Alt+Del"
g.Add("Text", "w260 y+10 cGray", helpText)
btnKb.OnEvent("Click", (*) => ToggleKb(true))
btnMouse.OnEvent("Click", (*) => ToggleMouse(true))
g.OnEvent("Close", (*) => (SetKb(false), SetMouse(false), ExitApp()))
g.Show()
UpdateStatus()
; ---------- Keyboard blocking ----------
kbKeys := []
Loop 247 {
vk := A_Index + 7
key := "$*vk" Format("{:02X}", vk)
try {
Hotkey key, (*) => "", "Off"
kbKeys.Push(key)
}
}
; ---------- Mouse click blocking ----------
mouseKeys := ["$*LButton", "$*RButton", "$*MButton", "$*XButton1", "$*XButton2", "$*WheelUp", "$*WheelDown", "$*WheelLeft", "$*WheelRight"]
for k in mouseKeys
Hotkey k, (*) => "", "Off"
; ---------- Unlock macros ----------
^!m:: SetMouse(false) ; Ctrl+Alt+M unlocks mouse
~RButton:: { ; triple right-click unlocks keyboard
global clickTimes
clickTimes.Push(A_TickCount)
if clickTimes.Length > 3
clickTimes.RemoveAt(1)
if clickTimes.Length = 3 && (clickTimes[3] - clickTimes[1] < 1200) && kbLocked {
clickTimes := []
SetKb(false)
}
}
; ---------- Functions ----------
ToggleKb(delay := false) {
if kbLocked {
SetKb(false)
return
}
StartLock(delay, () => SetKb(true))
}
ToggleMouse(delay := false) {
if mouseLocked {
SetMouse(false)
return
}
StartLock(delay, () => SetMouse(true))
}
StartLock(delay, fn) {
if delay {
status.Value := "Locking in 3 seconds..."
SetTimer () => (fn(), ArmSafety()), -3000
} else {
fn()
ArmSafety()
}
}
ArmSafety() {
secs := edSafety.Value = "" ? 0 : Integer(edSafety.Value)
if secs > 0
SetTimer SafetyUnlock, -secs * 1000
}
SafetyUnlock() {
SetKb(false)
SetMouse(false)
}
SetKb(state) {
global kbLocked := state
for key in kbKeys
Hotkey key, state ? "On" : "Off"
UpdateStatus()
}
SetMouse(state) {
global mouseLocked := state
for k in mouseKeys
Hotkey k, state ? "On" : "Off"
BlockInput state ? "MouseMove" : "MouseMoveOff"
UpdateStatus()
}
UpdateStatus() {
kbText := kbLocked ? "LOCKED" : "free"
msText := mouseLocked ? "LOCKED" : "free"
status.Value := Format("Keyboard: {1}`nMouse: {2}", kbText, msText)
btnKb.Text := kbLocked ? "UNLOCK Keyboard" : "Lock KEYBOARD"
btnMouse.Text := mouseLocked ? "UNLOCK Mouse" : "Lock MOUSE"
}