forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass_Hotkey.ahk
400 lines (306 loc) · 10.8 KB
/
Class_Hotkey.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
Class Hotkey {
static baseContext := {type: "IfWinActive", title: "A"}
portNumber := ""
, buttonNumber := ""
, ITERATOR_DELAY := ""
, ITERATOR_PERIOD := ""
__New(_keyName, _params*) {
static _a := (Hotkey["hk_group Default"]:={IfWinActive: {}, IfWinNotActive: {}, IfWinExist: {}, IfWinNotExist: {}, If: {}})
static _b := Hotkey.setContext()
static _devices := {0: "keyboard", 1: "joystick"}
_listLines := A_ListLines
ListLines, Off
if not (StrLen(_keyName:=Trim(_keyName)))
throw ErrorLevel := 2
for _index, _param in _params, _state := true {
if not (Bound.Func._isCallableObject(_param)) {
if not (_index = _params.length())
throw ErrorLevel := 1
_state := !!_params.pop()
} else this.callbacks.push(_params[_index]:=_param)
}
this.device := _devices[ !!InStr(_keyName, "Joy") ]
if (this.device = "joystick") {
if not (this._joyKeyValidator(_keyName, _portNumber, _buttonNumber, _isKeyUp))
throw ErrorLevel := 2
_keyName := this.keyName := (this.portNumber:=_portNumber) . "Joy" . (this.buttonNumber:=_buttonNumber), this._isKeyUp := _isKeyUp
Hotkey._createContext(this, _hotkeys, _context)
if not (_context.hasKey(_keyName) && (this._isKeyUp <> _context[_keyName]._isKeyUp)) {
this._isDualHotkey := false
this._boundIterators := {}, this.ITERATOR_PERIOD := 95, this.ITERATOR_DELAY := 300
this._boundIterators.joyDelay := new Bound.Func.Iterator(this, "_joyDelay")
if (_isKeyUp) {
_f := this._joyButtonW.bind(this)
this._boundIterators.joyWait := new Bound.Func.Iterator(this, "_joyWait")
} else {
_f := this._joyButtonR.bind(this)
this._boundIterators.joyRepeat := new Bound.Func.Iterator(this, "_joyRepeat")
, this.shouldIterate := true
}
} else {
this := _context[_keyName]
this._isDualHotkey := true
this.__press := this._callCallbackChain.bind({"callbacks": this.callbacks})
, this.__released := this._callCallbackChain.bind({"callbacks": _params})
_f := this._joyButtonR.bind(this)
this._boundIterators.joyRepeat.delete(), this._boundIterators.joyRepeat := new Bound.Func.Iterator(this, "_joyRepeat2")
, this.shouldIterate := true
}
if not (this._apply(_f))
throw ErrorLevel
} else {
if ((this.keyName:=Hotkey._normalize(_keyName)) = "")
throw ErrorLevel
Hotkey._createContext(this, _hotkeys)
if not (this._apply(this._callCallbackChain.bind(this)))
throw ErrorLevel
}
this._enable(_state)
ListLines % _listLines ? "On" : "Off"
return _hotkeys[ this.context.type, this.context.title, this.keyName ] := this
}
delete() {
static _junkFunc := Func("WinActive")
if (this.disable()) {
Hotkey.setContext(this.context.type, this.context.title)
if (this._apply(_junkFunc)) {
return this._remove(), Hotkey.setContext(Hotkey.baseContext.type, Hotkey.baseContext.title)
}
}
Hotkey.setContext(Hotkey.baseContext.type, Hotkey.baseContext.title)
return 0
}
enable(_boolean:=true) {
Hotkey.setContext(this.context.type, this.context.title)
if (this._apply(_boolean ? "On" : "Off")) {
Hotkey.setContext(Hotkey.baseContext.type, Hotkey.baseContext.title)
return true, this.enabled:=_boolean
}
return false, Hotkey.setContext(Hotkey.baseContext.type, Hotkey.baseContext.title)
}
disable() {
Hotkey.setContext(this.context.type, this.context.title)
if (this._apply("Off")) {
Hotkey.setContext(Hotkey.baseContext.type, Hotkey.baseContext.title)
return true, this.enabled:=false
}
return false, Hotkey.setContext(Hotkey.baseContext.type, Hotkey.baseContext.title)
}
onReleased(_callbacks*) {
if ((this.device = "joystick") && !this._isKeyUp)
_callbacks.push(this.enabled)
return new Hotkey(this.keyName . " Up", _callbacks*)
throw Exception(-2)
}
; ===== CALLED VIA BASE OBJECT /=====
disableAll(_group:="") {
Hotkey._callAll("_disable", _group)
}
enableAll(_group:="") {
Hotkey._callAll("_enable", _group)
}
deleteAll(_group:="") {
Hotkey._callAll("_delete", _group)
}
setGroup(_group:="") {
if (StrLen(_group)) {
if not (Hotkey.hasKey(_group:=Hotkey._group:="hk_group " . _group)) {
Hotkey[_group] := {IfWinActive: {}, IfWinNotActive: {}, IfWinExist: {}, IfWinNotExist: {}, If: {}}
}
} else Hotkey._group := "hk_group Default"
}
setContext(_type:="IfWinActive", _winTitle:="A") {
if not (Trim(_type) ~= "^(IfWinActive|IfWinNotActive|IfWinExist|IfWinNotExist|If)$")
throw Exception(-3)
Hotkey.baseContext.title:=_winTitle
Hotkey, % Hotkey.baseContext.type:=_type, % _winTitle
}
clearContext() {
Hotkey.setContext()
}
; =====/ CALLED VIA BASE OBJECT =====
; ===== PRIVATE /=====
static _group := "hk_group Default"
callbacks := []
, _boundIterators := {}
_createContext(_instance, ByRef _hotkeys:="", ByRef _context:="") {
_hotkeys := Hotkey.hasKey(_instance.group:=Hotkey._group) ? Hotkey[ _instance.group ] : (Hotkey[ _instance.group ]:={})
, _baseContext := Hotkey.baseContext, _bType := _baseContext.type, _bTitle := _baseContext.title, _instance.context := {}
if ((_context:=_hotkeys[ _bType ]).hasKey(_bTitle) and !(_context:=_context[ _bTitle]).hasKey(this.keyName)) {
_instance.context.type := _bType, _instance.context.title := _bTitle
} else _context := _hotkeys[ _instance.context.type:=_bType, _instance.context.title:=_bTitle ] := {}
}
_apply(_func) {
Hotkey, % this.keyName, % _func, UseErrorLevel
(ErrorLevel = 2 && ErrorLevel := 0)
return !ErrorLevel
}
_callCallbackChain() {
for _each, _callback in this.callbacks
_callback.call()
}
_remove() {
this.callbacks := "", this.__press := this.__released := ""
for _method, _boundIterator in this._boundIterators
_boundIterator.delete()
_hotkeys := Hotkey[ this.group ]
if not ((_context:=_hotkeys[_type:=this.context.type, _title:=this.context.title]).hasKey(this.keyName))
return -1
_context.remove(this.keyName), _l := NumGet(&_context, 4 * A_PtrSize)
if not (--_l)
_hotkeys[_type].remove(_title)
return 1
}
_delete() {
static _junkFunc := Func("WinActive")
if (this._disable())
if (this._apply(_junkFunc))
return 1
return 0
}
_enable(_boolean:=true) {
return (this._apply((this.enabled:=_boolean) ? "On" : "Off"))
}
_disable() {
return (this._apply("Off"))
}
_callAll(_method, _group) {
_type := Hotkey.baseContext.type, _title := Hotkey.baseContext.title
if (StrLen(_group)) {
if not (Hotkey.hasKey(_group:="hk_group " . _group))
throw Exception(-1)
for _hkType, _obj1 in Hotkey[_group], _instances := [] {
for _hkTitle, _obj2 in _obj1 {
Hotkey.setContext(_hkType, _hkTitle)
for _, _hotkey in _obj2 {
_instances.push(_hotkey), _hotkey[_method].call(_hotkey)
}
}
}
} else {
for _hkGroup, _obj1 in Hotkey, _instances := [] {
for _hkType, _obj2 in _obj1 {
for _hkTitle, _obj3 in _obj2 {
Hotkey.setContext(_hkType, _hkTitle)
for _, _hotkey in _obj3 {
_instances.push(_hotkey), _hotkey[_method].call(_hotkey)
}
}
}
}
}
Hotkey.setContext(_type, _title)
if (_method = "_delete")
for _index, _hotkey in _instances
_hotkey._remove()
}
_joyButtonR() {
(this._isDualHotkey) ? this.__press.call() : this._callCallbackChain.bind(this).call()
, this._boundIterators.joyDelay.setPeriod(-this.ITERATOR_DELAY)
}
_joyDelay() {
if (this.shouldIterate && GetKeyState(this.keyName))
this._boundIterators.joyRepeat.setPeriod(this.ITERATOR_PERIOD), this.shouldIterate := false
else if (this._isDualHotkey)
this.__released.call()
}
_joyRepeat() {
GetKeyState(this.keyName) ? this._callCallbackChain.bind(this).call()
: (this._boundIterators.joyRepeat.setPeriod("Off"), this.shouldIterate := true)
}
_joyRepeat2() {
GetKeyState(this.keyName) ? this.__press.call()
: (this._boundIterators.joyRepeat.setPeriod("Off"), this.__released.call(), this.shouldIterate := true)
}
_joyButtonW() {
this._boundIterators.joyWait.setPeriod(25)
}
_joyWait() {
(GetKeyState(this.keyName)) ? this._boundIterators.joyWait.setPeriod(25)
: (this._boundIterators.joyWait.setPeriod("Off"), this._callCallbackChain.bind(this).call())
}
_normalize(_hotkey, ByRef _useHook:="", ByRef _hasTilde:="") { ; cf. https://github.com/Lexikos/xHotkey.ahk/blob/master/Lib/HotkeyNormalize.ahk
static _allMods := StrSplit("* <^ <! <+ <# >^ >! >+ ># ^ ! + #", " ")
if (_p:=InStr(_hotkey, " & "))
return Hotkey._normalize(RTrim(SubStr(_hotkey, 1, _p))) . " & " . Hotkey._normalize(LTrim(SubStr(_hotkey, _p+3)))
_hotkey := RegExReplace(_hotkey, "i)[ `t]Up$", "", _isKeyUp, 1)
if not (_p:=RegExMatch(_hotkey, "^[~$*<>^!+#]*\K(\w+|.)$"))
return "", ErrorLevel:=2
_mods := SubStr(_hotkey, 1, _p-1)
if (_useHook:=InStr(_mods, "$") != 0)
_mods := StrReplace(_mods, "$")
if (_hasTilde:=InStr(_mods, "~") != 0)
_mods := StrReplace(_mods, "~")
_sortedMods := ""
if (_mods)
for _each, _aMod in _allMods
if (InStr(_mods, _aMod))
_sortedMods .= _aMod, _mods := StrReplace(_mods, _aMod)
_key := SubStr(_hotkey, _p)
if (_key ~= "i)^(.$|vk|sc)")
_key := Format("{:L}", _key)
else if (_n:=GetKeyName(_key))
_key := _n
else return "", ErrorLevel:=2
return _sortedMods . _key . (_isKeyUp ? " Up" : "")
}
_joyKeyValidator(_joyKeyName, ByRef _portNumber:="", ByRef _buttonNumber:="", ByRef _isKeyUp:="") {
if (RegExMatch(Trim(_joyKeyName), "Oi)^(?P<joystickPort>\d{1,2})?Joy((?P<buttonNumber>\d{1,2})(\s+(?P<isKeyUp>Up))?)$", _match)) {
if ((_portNumber:=_match.value("joystickPort")) = "") {
Loop 16 {
if (GetKeyState(a_index . "JoyX")) {
_portNumber := a_index
break
}
}
}
if _portNumber not between 1 and 16
return false
_buttonNumber := _match.value("buttonNumber"), _isKeyUp := !!_match.value("isKeyUp")
if _buttonNumber not between 1 and 32
return false
return true
}
return false
}
; =====/ PRIVATE =====
}
Class Bound {
Class Func { ; cf. https://github.com/Lexikos/xHotkey.ahk/blob/master/xHotkey_test.ahk
__New(_fn, _args*) {
if not (_v:=Bound.Func._isCallableObject(_fn))
throw ErrorLevel:=1
else this.fn := (_v < 1) ? _fn.bind(_args*) : ObjBindMethod(_fn, _args.removeAt(1), _args*)
}
__Call(_callee) {
if (StrReplace(_callee, "call", "") = "") {
_fn := this.fn
return %_fn%()
}
}
_isCallableObject(ByRef _callback) {
if (IsFunc(_callback)) {
((_callback.minParams = "") && _callback:=Func(_callback))
return -1
} else if (IsObject(_callback)) ; _callback.base.hasKey("__Call")
return 1
else return 0
}
Class Iterator {
__New(_fn, _args*) {
this.callableObject := new Bound.Func(_fn, _args*)
}
setPeriod(_period) {
if (_f:=this.callableObject)
SetTimer, % _f, % _period
}
delete() {
if not (_f:=this.callableObject)
return
SetTimer, % _f, Off
SetTimer, % _f, Delete
return this.callableObject := ""
}
}
}
}