-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_InputBlocker.ahk
100 lines (90 loc) · 2.71 KB
/
class_InputBlocker.ahk
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
#include <CallbackCreate>
;-----------------------------------------------------------------------
; InputBlocker
; Blocks user input until a password is entered, or Stop() is called.
;-----------------------------------------------------------------------
class InputBlocker {
static WH_KEYBOARD_LL := 13
static WH_MOUSE_LL := 14
static WM_KEYUP := 0x101
; takes a string or an array of character keys
__New(_password:="abc123") { ; default password
this.password := IsObject(_password) ? _password : StrSplit(_password)
if (this.password.Count() < 1)
throw Exception("Invalid password.")
this.callback := CallbackCreate(ObjBindMethod(this, "_hookProc"), , 3) ; num params = 3
this.keyboardHook := ""
this.mouseHook := ""
this.pos := 1
this.passwordEntered := false
}
__Delete() {
CallbackFree(this.callback)
}
Start() {
this.pos := 1
this.passwordEntered := false
this.Stop()
this.keyboardHook := this._setWindowsHookEx(InputBlocker.WH_KEYBOARD_LL, this.callback)
this.mouseHook := this._setWindowsHookEx(InputBlocker.WH_MOUSE_LL, this.callback)
;~ this._releaseAllKeys() ; might not be needed
}
Stop() {
if (this.keyboardHook)
this._unhookWindowsHookEx(this.keyboardHook)
if (this.mouseHook)
this._unhookWindowsHookEx(this.mouseHook)
}
; checks if block was interrupted and clears the flag
IsInterrupted() {
Critical
_retval := this.passwordEntered
this.passwordEntered := false
return _retval
}
_hookProc(nCode, wParam, lParam) {
Critical 100
if (nCode >= 0) {
if (wParam = InputBlocker.WM_KEYUP) {
if ((NumGet(lParam+0) << 32 >> 32) = GetKeyVK(this.password[this.pos])) {
if (this.pos++ >= this.password.MaxIndex()) {
this.passwordEntered := true
this.pos := 1
this.Stop()
}
}
else {
FileAppend, % "Blocked " (NumGet(lParam+0) << 32 >> 32), *
this.pos := 1
}
}
return 1 ; blocks the message
}
else return this._callNextHookEx(nCode, wParam, lParam)
}
_releaseAllKeys() {
_intMode := A_FormatInteger
_s := ""
SetFormat, IntegerFast, H
loop, 256 {
if GetKeyState("VK" hexCode := SubStr(A_Index-1, 3))
_s .= "{vk" hexCode " up}"
}
if _s
Send, %s%
SetFormat, IntegerFast, %_intMode%
return s
}
_setWindowsHookEx(idHook, addr) {
static PTR := A_PtrSize ? "Ptr" : "UInt"
return DllCall("SetWindowsHookEx", "Int", idHook, "UInt", addr, PTR, 0, "UInt", 0, PTR)
}
_unhookWindowsHookEx(hHook) {
static PTR := A_PtrSize ? "Ptr" : "UInt"
return DllCall("UnhookWindowsHookEx", PTR, hHook)
}
_callNextHookEx(nCode, wParam, lParam, kHook = 0) {
static PTR := A_PtrSize ? "Ptr" : "UInt"
return DllCall("CallNextHookEx", PTR, kHook, "Int", nCode, "UInt", wParam, "UInt", lParam)
}
}