-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_RHotkey.ahk
173 lines (138 loc) · 3.65 KB
/
class_RHotkey.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
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
/*
Class RHotkey ; Modified class name form RHotkey to RHotkey by Boulmers
Written by: Runar "RUNIE" Borge with help from A_AhkUser
Usage: check out the example.
Methods:
__New(Key, Target [, Window, Type]) - create a new hotkey instance
Key - The key to bind
Target - Function referece, boundfunc or label name to run when Key is pressed
Window - *OPTIONAL* The window the hotkey should be related to
Type - *OPTIONAL* What context the hotkey has to the window (Active, Exist, NotActive, NotExist)
Delete() - Delete hotkey. Do this when you're done with the hotkey. It differs from Disable() since it releases the object the key is bound to.
Enable() - Enable hotkey
Disable() - Disable hotkey
Toggle() - Enable/Disable toggle
Base object methods:
RHotkey.GetKey(Key, Window [, Type]) - Parameters are the same as in __New() without the Target param.
RHotkey.DeleteAll() - Delete all hotkeys
RHotkey.EnableAll() - Enable all disabled hotkeys
RHotkey.DisableAll() - Disable all enabled hotkeys
*/
; all methods return false on failure
Class RHotkey
{
static Keys := {} ; keep track of instances
static KeyEnabled := {} ; keep track of state of hotkeys
; create a new hotkey
__New(Key, Target, Window := false, Type := "Active")
{
; check input
if !StrLen(Window)
Window := false
if !StrLen(Key)
return false, ErrorLevel := 2
if !(Bind := IsLabel(Target) ? Target : this.CallFunc.Bind(this, Target))
return false, ErrorLevel := 1
if !(Type ~= "im)^(Not)?(Active|Exist)$")
return false, ErrorLevel := 4
; set values
this.Key := Key
this.Window := Window
this.Type := Type
; enable if previously disabled
if ( RHotkey.KeyEnabled[Type, Window, Key] = false )
this.Apply("On")
; bind the key
if !this.Apply(Bind)
return false
this.Enabled := true ; set to enabled
return RHotkey.Keys[Type, Window, Key] := this
}
; 'delete' a hotkey. call this when you're done with a hotkey
; this is superior to Disable() as it releases the function references
Delete()
{
static JunkFunc := Func("WinActive")
if this.Disable()
if this.Apply(JunkFunc)
return true, RHotkey.Keys[this.Type, this.Window].Remove(this.Key)
return false
}
; enable hotkey
Enable()
{
if this.Apply("On")
return true, this.Enabled := true
return false
}
; disable hotkey
Disable()
{
if this.Apply("Off")
return true, this.Enabled := false
return false
}
; toggle enabled/disabled
Toggle()
{
return this[this.Enabled ? "Disable" : "Enable"].Call(this)
}
; ===== CALLED VIA BASE OBJECT =====
; enable all hotkeys
EnableAll()
{
RHotkey.CallAll("Enable")
}
; disable all hotkeys
DisableAll()
{
RHotkey.CallAll("Disable")
}
; delete all hotkeys
DeleteAll()
{
RHotkey.CallAll("Delete")
}
; get a hotkey instance from it's properties
GetKey(Key, Window := false, Type := "Active")
{
return RHotkey.Keys[Type, Window, Key]
}
; ===== PRIVATE =====
Enabled[]
{
get {
return RHotkey.KeyEnabled[this.Type, this.Window, this.Key]
}
set {
return RHotkey.KeyEnabled[this.Type, this.Window, this.Key] := value
}
}
CallFunc(Target)
{
Target.Call()
}
Apply( Label_ )
{
Hotkey, % "IfWin" this.Type, % this.Window ? this.Window : ""
if ErrorLevel
return false
Hotkey, % this.Key, % Label_, UseErrorLevel
if ErrorLevel
{
_Logger.ERROR(A_ThisFunc, "Key", this.Key, "Label_", Label_, "ErrorLevel", ErrorLevel)
return false
}
return true
}
CallAll(Method)
{
Instances := []
for Index, Type in RHotkey.Keys
for Index, Window in Type
for Index, Htk in Window
Instances.Push(Htk)
for Index, Instance in Instances
Instance[Method].Call(Instance)
}
}