-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xarty-extend-v3.ahk
504 lines (458 loc) · 10.3 KB
/
Xarty-extend-v3.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#Requires AutoHotkey >= 2
A_MaxHotkeysPerInterval := 150
A_HotkeyInterval := 1000
SetMouseDelay 1
SetDefaultMouseSpeed 2
SetKeyDelay -1
SendMode "Event"
ProcessSetPriority "A"
FileEncoding "UTF-8" ; https://www.autohotkey.com/docs/v2/lib/File.htm#Encoding
; extendKey := "CapsLock"
; extendLayer1Key := "Shift"
; extendLayer2Key := "Ctrl"
if FileExist("xarty_config.ini") == ""
{
FileAppend "
(
[xarty-global]
; Upon the Compose sequence complete typing,
; the window for you to press Compose key
; for the ranslation to begin. (in millisecond)
; This property should be a positive integer,
; otherwise invalid.
intervalAllowedForComposeValidation = 4800
; When you press and release a modifer key,
; the window for you to press extend key that
; will still activate a extend layer. (in millisecond)
; This property should be a positive integer,
; otherwise invalid.
intervalAllowedForExtendLayerActivation = 250
)", "xarty_config.ini", "UTF-16"
}
if FileExist("compose.txt") == ""
{
FileAppend "
(
; This file is used to create compose key pairs
; For details, specification, and guide of modification, refer to https://github.com/CarrieForle/xarty/wiki/Xarty-with-AHK#composetxt
=btw=By the way
=name=CarrieForle
=lol=(゚∀。)
)", "compose.txt"
}
beepHelper() {
if A_IsSuspended
SoundBeep 1600, 75
else
SoundBeep 1100, 75
}
try
{
intervalAllowedForComposeValidation := Integer(IniRead("xarty_config.ini", "xarty-global", "intervalAllowedForComposeValidation")),
intervalAllowedForExtendLayerActivation := Integer(IniRead("xarty_config.ini", "xarty-global", "intervalAllowedForExtendLayerActivation"))
beepOnToggleScriptSuspension := IniRead("xarty_config.ini", "xarty-global", "beepOnToggleScriptSuspension")
if beepOnToggleScriptSuspension = "true"
toggleSuspension := () => (
beepHelper(),
Suspend(-1)
)
else if beepOnToggleScriptSuspension = "false"
toggleSuspension := () => suspend(-1)
else
throw ValueError("")
if intervalAllowedForComposeValidation <= 0 || intervalAllowedForExtendLayerActivation <= 0
throw ValueError("")
}
catch Error as e
{
MsgBox e.Message . "Invalid properties found in xarty_config.ini.`nThe program will be terminated."
ExitApp
}
timeSinceLastKey := -intervalAllowedForComposeValidation - 1,
timeSinceExtendPrestart := -intervalAllowedForExtendLayerActivation - 1,
maximumComposeKeyLength := 20,
modifierHotKey := ""
wordList := Array()
wordList.Length := wordList.Capacity := maximumComposeKeyLength
loop wordList.Length
wordList[A_Index] := Map()
Loop Read "compose.txt"
{
if A_LoopReadLine == ""
continue
delimiterChar := SubStr(A_LoopReadLine, 1, 1)
if delimiterChar == ";" ||
delimiterChar == "`n" ||
delimiterChar == A_Tab ||
delimiterChar == A_Space
continue
keypair := StrSplit(A_LoopReadLine, delimiterChar,, 3)
if keypair.Length < 3 || keypair[2] == "" ||keypair[3] == ""
{
if "No" == MsgBox(A_LoopReadLine " is not a valid compose keypair.`n`nClick `"Yes`" to cuntinue and ignore this keypair.`nClick `"No`" to terminate the script.", "Error in compose.txt", 4)
ExitApp
}
else if StrLen(keypair[2]) > maximumComposeKeyLength
{
if "No" == MsgBox(A_LoopReadLine " is too long for a key (> 10).`n`nClick `"Yes`" to cuntinue and ignore this keypair.`nClick `"No`" to terminate the script.", "Error in compose.txt", 4)
ExitApp
}
else
{
wordList[maximumComposeKeyLength - StrLen(keypair[2]) + 1].Set(keypair[2], keypair[3])
}
}
VarSetStrCapacity &keypair, 0
if GetKeyState("CapsLock", "T")
SetCapsLockState "AlwaysOn"
else
SetCapsLockState "AlwaysOff"
#InputLevel 1
#HotIf modifierHotKey == "~Shift up" && A_TickCount - timeSinceExtendPrestart <= intervalAllowedForExtendLayerActivation
CapsLock::
{
SendEvent("{vk3c DownR}{vk3d Up}{vk3e Up}"),
KeyWait("CapsLock")
}
#HotIf modifierHotKey == "~Ctrl up" && A_TickCount - timeSinceExtendPrestart <= intervalAllowedForExtendLayerActivation
CapsLock::
{
SendEvent("{vk3c Up}{vk3d DownR}{vk3e Up}"),
KeyWait("CapsLock")
}
#HotIf !GetKeyState("CapsLock", "P")
~Ctrl up::
~Shift up::
{
global
timeSinceExtendPrestart := A_TickCount,
modifierHotKey := ThisHotKey
}
#HotIf
CapsLock & Shift::
+CapsLock::
{
SendEvent("{vk3c DownR}{vk3d Up}{vk3e Up}"),
KeyWait("CapsLock")
}
CapsLock & Ctrl::
^CapsLock::
{
SendEvent("{vk3c Up}{vk3d DownR}{vk3e Up}"),
KeyWait("CapsLock")
}
^+CapsLock::
~CapsLock::
{
SendEvent("{vk3c Up}{vk3d Up}{vk3e DownR}"),
KeyWait("CapsLock")
}
~CapsLock up::
{
SendEvent "{vk3c Up}{vk3d Up}{vk3e Up}"
}
#SuspendExempt
#Hotif A_IsSuspended
CapsLock::CapsLock ; for CapsLock to be toggleabde while suspending
#HotIf
RAlt & LAlt::
LAlt & RAlt::toggleSuspension
^+sc006::
{
SendEvent "{vk3c Up}{vk3d Up}{vk3e Up}"
SoundBeep 3000, 30
Reload
SoundBeep 4500, 30
}
^+sc029::ExitApp
#SuspendExempt false
#InputLevel 0
HoldKey(key) {
SendInput "{Blind}{" key " DownR}"
KeyWait key, "L"
}
HoldKeyNormal(key) {
SendInput "{Blind}{" key " Down}"
KeyWait key, "L"
}
vk3c & sc002::!
vk3c & sc003::@
vk3c & sc004::#
vk3c & sc005::$
vk3c & sc006::%
vk3c & sc007::^
vk3c & sc008::Numpad7
vk3c & sc009::Numpad8
vk3c & sc00a::Numpad9
vk3c & sc00b::NumpadMult
vk3c & sc00c::NumpadSub
vk3c & sc00d::=
vk3c & sc010::Home
vk3c & sc011::Up
vk3c & sc012::End
vk3c & sc013::Del
vk3c & sc014::Esc
vk3c & sc015::PgUp
vk3c & sc016::Numpad4
vk3c & sc017::Numpad5
vk3c & sc018::Numpad6
vk3c & sc019::NumpadAdd
vk3c & sc01a::(
vk3c & sc01b::)
vk3c & sc02b::,
vk3c & sc01e::Left
vk3c & sc01f::Down
vk3c & sc020::Right
vk3c & sc021::Backspace
vk3c & sc022::NumLock
vk3c & sc023::PgDn
vk3c & sc024::Numpad1
vk3c & sc025::Numpad2
vk3c & sc026::Numpad3
vk3c & sc027::NumpadEnter
vk3c & sc028::'
;'
vk3c & sc02c::^z
vk3c & sc02d::^x
vk3c & sc02e::^c
vk3c & sc02f::^v
vk3c & sc030::LButton
vk3c & sc031:::
vk3c & sc032::Numpad0
vk3c & sc033::Numpad0
vk3c & sc034::NumpadDot
vk3c & sc035::NumpadDiv
vk3d & sc029::
vk3d & sc002::
vk3d & sc003::
vk3d & sc004::
vk3d & sc005::
vk3d & sc006::
vk3d & sc007::
vk3d & sc008::
vk3d & sc009::
vk3d & sc00a::
vk3d & sc00b::
vk3d & sc00c::
vk3d & sc00d::return
vk3d & sc010::[
vk3d & sc011::]
vk3d & sc012::~
vk3d & sc013::
vk3d & sc014::
vk3d & sc015::return
vk3d & sc016::'
;'
vk3d & sc017::"
;"
vk3d & sc018::\
vk3d & sc019::
vk3d & sc01a::
vk3d & sc01b::
vk3d & sc02b::return
vk3d & sc01e::(
vk3d & sc01f::)
vk3d & sc020::`
vk3d & sc021::
vk3d & sc022::
vk3d & sc023::return
vk3d & sc024::`{
vk3d & sc025::}
vk3d & sc026::%
vk3d & sc027::!
vk3d & sc028::return
vk3d & sc02c::&
vk3d & sc02d::|
vk3d & sc02e::*
vk3d & sc02f::
vk3d & sc030::
vk3d & sc031::return
vk3d & sc032::+
vk3d & sc033::-
vk3d & sc034::=
vk3d & sc035::return
vk3e & Esc::CapsLock
vk3e & F1::HoldKey "Media_Play_Pause"
vk3e & F1 up::Send "{blind}{Media_Play_Pause Up}"
vk3e & F2::HoldKey "Media_Prev"
vk3e & F2 up::Send "{blind}{Media_Prev Up}"
vk3e & F3::HoldKey "Media_Next"
vk3e & F3 up::Send "{blind}{Media_Next Up}"
vk3e & F4::HoldKey "Media_Stop"
vk3e & F4 up::Send "{blind}{Media_Stop Up}"
vk3e & F5::HoldKey "Volume_Mute"
vk3e & F5 up::Send "{blind}{Volume_Mute Up}"
vk3e & F6::Volume_Down
vk3e & F7::Volume_Up
vk3e & F8::HoldKey "Launch_Media"
vk3e & F8 up::Send "{blind}{Launch_Media Up}"
vk3e & sc029::SendEvent "{Click 16 96 0}"
vk3e & sc002::F1
vk3e & sc003::F2
vk3e & sc004::F3
vk3e & sc005::F4
vk3e & sc006::F5
vk3e & sc007::F6
vk3e & sc008::F7
vk3e & sc009::F8
vk3e & sc00a::F9
vk3e & sc00b::F10
vk3e & sc00c::F11
vk3e & sc00d::F12
vk3e & sc010::Esc
vk3e & sc011::WheelUp
vk3e & sc012::Browser_Back
vk3e & sc013::Browser_Forward
vk3e & sc014::SendInput "{Click 0 -20 0 Rel}"
vk3e & sc015::PgUp
vk3e & sc016::Home
vk3e & sc017::Up
vk3e & sc018::End
vk3e & sc019::Del
vk3e & sc01a::WheelLeft
vk3e & sc01b::WheelRight
vk3e & sc01e::Alt
vk3e & sc01f::WheelDown
vk3e & sc020::Shift
vk3e & sc021::Ctrl
vk3e & sc022::SendInput "{Click 0 20 0 Rel}"
vk3e & sc023::PgDn
vk3e & sc024::Left
vk3e & sc025::Down
vk3e & sc026::Right
vk3e & sc027::BackSpace
vk3e & sc028::HoldKey "AppsKey"
vk3e & sc028 up::Send "{blind}{AppsKey Up}"
vk3e & sc02c::^z
vk3e & sc02d::^x
vk3e & sc02e::^c
vk3e & sc02f::^v
vk3e & sc030::Ins
vk3e & sc031::LButton
vk3e & sc032::MButton
vk3e & sc033::RButton
vk3e & sc034::SendInput "{Click -42 0 0 Rel}"
vk3e & sc035::SendInput "{Click 42 0 0 Rel}"
vk3e & Enter::^BackSpace
vk3e & Space::Enter
sc029::`
sc002::1
sc003::2
sc004::3
sc005::4
sc006::5
sc007::6
sc008::7
sc009::8
sc00a::9
sc00b::0
sc00c::-
sc00d::=
sc010::v
sc011::h
sc012::s
sc013::g
sc014::b
sc015::j
sc016::m
sc017::o
sc018::u
sc019::;
sc01a::[
sc01b::]
sc02b::\
sc01e::x
sc01f::a
sc020::r
sc021::t
sc022::y
sc023::k
sc024::n
sc025::e
sc026::i
sc027::w
sc028::'
;'
sc02c::z
sc02d::f
sc02e::l
sc02f::d
sc030::q
sc031::c
sc032::p
sc033::,
sc034::.
sc035::/
ih := InputHook("V L" . maximumComposeKeyLength, "{Left}{Up}{Right}{Down}{Home}{PgUp}{End}{PgDn}"),
oldBuffer := ""
sc15d::
{
if A_PriorHotKey !== ThisHotKey
onKeyDown(ih, 0x5d, 0x15d)
KeyWait "sc15d"
}
~Backspace::
~+Backspace::
{
global oldBuffer
if ih.Input == "" && oldBuffer != ""
oldBuffer := SubStr(oldBuffer, 1, StrLen(oldBuffer) - 1)
}
~!Backspace::
~*^Backspace::
{
if A_PriorHotKey != "~!Backspace" && A_PriorHotKey != "~*^Backspace"
{
ih.Stop(),
ih.Start()
}
}
onChar(ih, ch)
{
global timeSinceLastKey := A_TickCount
}
onKeyDown(ih, vk, sc)
{
global timeSinceLastKey
if A_TickCount - timeSinceLastKey > intervalAllowedForComposeValidation
{
ih.Stop(),
ih.Start()
}
else if A_TickCount - timeSinceLastKey <= intervalAllowedForComposeValidation
{
inpBuffer := oldBuffer . ih.Input
for words in wordList
{
for key, val in words
{
if key == SubStr(inpBuffer, -StrLen(key))
{
ih.Stop(),
SendInput("{Backspace " StrLen(key) "}" val),
ih.Start()
return
}
}
}
}
}
onEnd(ih)
{
global oldBuffer
if ih.EndReason == "Max"
{
oldBuffer := ih.Input
ih.Start()
}
else
{
if ih.EndReason == "EndKey"
ih.Start()
oldBuffer := ""
}
}
ih.OnKeyDown := onKeyDown,
ih.OnEnd := onEnd,
ih.OnChar := onChar,
ih.Start()