-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboard.ahk
1811 lines (1656 loc) · 47.6 KB
/
keyboard.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires AutoHotkey v2.0
#SingleInstance force ;; Auto-reload
#Warn ;; Enable warnings, show message box.
;;. Using space as the dual mode key for alt does not work: writing
;; text fast always triggers alt-key hotkeys (google docs etc).
;;. Using caps lock as dual mode key for backspace does not work since
;; it's often mispressed and "backspace" do terrible things like
;; deleting items and so on.
;;. Using 'caps lock' as leader is better than 'tab' since leader is
;; used a lot to emulate arrows, wheel and clicks, while left control
;; (which caps lock originally was replaced to) is used much less,
;; mostly as C-[ for escape in VIM, but leader-[ is also an escape,
;; so nothing changes.
;;. 'ctrl-;' is used in Chrome for debugging, so it's not used as
;; ctrl-click shortcut which is rare and 'tab+ctrl' can be holded for it.
;;. leaving backspace "as is" simply don't work: it's too far away.
;;. Windows detects physical shift while pressing shift-del, so keeping
;; "del" as leader-shift-/ is not viable, it is detected as "skip recycle
;; bin". Instead, leader-p is mapped to backspace, leader-shift-p to
;; ctrl-backspace (delete word) and leader-p to "delete".
;;. DllCall("mouse_event", "UInt", 0x800, "UInt", 0, "UInt", 0, "UInt", 120)
;; for mouse wheel will not work, on modern Windows it was replaced with
;; SendInput.
;;. It's better to remap 'caps lock' to some not-used keys (like F24)
;; using Windows registry and use that resulting key. Such trick prevents
;; caps lock from triggering in situations where keyboard hook is not
;; working (UAC, lock screen, "Grim Dawn" etc).
;;. '/' is much better fit for middle mouse button since triple-mode 'enter'
;; is overkill.
;;. One-finger scroll is used too often for m1-]: too much load for pinkey,
;; so it's better to move it to something that is easier to hold, ex
;; m1-.
;;. Unlike MacOS where hammerspoon can click any dock item, Windows
;; can switch only between 10 taskbar apps by itself, so mapping m1-plus
;; for 11-th app is not easy.
;;. For games like WoW right buttons hold are used for movement, so
;; sometimes caps lock is released while holding tick or semicolon. Holding
;; caps lock again should enter button hold (ex pressing m1 while holding
;; the corresponding key should emit keydown again). Current implementation
;; simply does not release mouse button while releasing mod.
codepage := 65001 ; utf-8
;; Reliable key state detection
InstallKeybdHook
;; Map of all remap configurations
appRemap := Map()
appMeta := Map()
appKeysPressed := Map()
appLastLang := ""
appLastDebug := ""
appIsDebug := false
appDebugLog := []
appSymbols := []
appLastKeydown := ["", Map()]
appLastKeyup := ["", Map()]
appLastCfg := ""
MAX_DEBUG_LOG := 50
KEY_NAMES := Map(
"vkc0", "~",
"vk8", "backspace",
"vkdb", "[",
"vkdd", "]",
"vkdc", "\",
"vked", "caps",
"vkba", ";",
"vkde", "'",
"vkbc", ",",
"vkbe", ".",
"vkbf", "/"
)
if (!A_IsAdmin) {
Run "*RunAs" A_ScriptFullPath
ExitApp
}
;; No warning if key is hold for 2 seconds (HotkeyInterval)
A_MaxHotkeysPerInterval := 500
assert(condition, msg) {
if (not condition) {
OutputDebug("ahk assert: " . msg)
}
}
repeatStr(times, str) {
res := ""
loop times
res .= str
return res
}
joinWithSep(sep, collection) {
res := ""
for _, item in collection {
if (res) {
res .= sep
}
res .= item
}
return res
}
includes(container, needles*) {
if (needles.Length == 1) {
needle := needles[1]
for _, val in container {
if val == needle {
return true
}
}
}
else {
found := 0
for _, val in container {
for _, needle in needles {
if val == needle {
found += 1
if (found == needles.Length) {
return true
}
}
}
}
}
return false
}
remove(container, needle) {
for idx, val in container {
if val == needle {
container.RemoveAt(idx)
return
}
}
}
mapToStr(container, oneline := false, indent := 0) {
res := "{"
if (not oneline) {
res .= "`n"
}
for key, val in container {
if (oneline) {
padding := ""
}
else {
padding := repeatStr(indent + 2, " ")
}
if (IsObject(val)) {
res .= padding . key . ": " . mapToStr(val, oneline, indent + 2) . ","
} else {
if (Type(val) == "String") {
res .= padding . key . ": `"" . val . "`","
} else {
res .= padding . key . ": " . val . ","
}
}
if (oneline) {
res .= " "
}
else {
res .= "`n"
}
}
return res . repeatStr(indent, " ") . "}"
}
arrayToStr(container) {
res := "["
for idx, val in container {
if (idx > 1) {
res .= ", "
}
if (Type(val) == "String") {
res .= "`"" . val . "`""
}
else {
res .= val
}
}
res .= "]"
return res
}
joinToStr(array, quote := false, oneline := false) {
res := ""
for idx, param in array {
if (idx > 1) {
res .= " "
}
if (Type(param) == "String") {
if (quote) {
res .= "`"" . param . "`""
}
else {
res .= param
}
}
else if (Type(param) == "Array") {
res .= arrayToStr(param)
}
else if (Type(param) == "Map") {
res .= mapToStr(param, oneline)
}
else if (Type(param) == "RegExMatchInfo") {
res .= "RegExMatchInfo(count=" . param.Count . ")"
}
else {
res .= param
}
}
return res
}
sortArray(&items) {
left := 1
right := items.Length
while (left < right) {
minIdx := left
search := left + 1
while (search <= right) {
if (items[search] < items[minIdx]) {
minIdx := search
}
search += 1
}
if (minIdx != left) {
tmp := items[left]
items[left] := items[minIdx]
items[minIdx] := tmp
}
left += 1
}
}
compareArray(left, right) {
if (left.Length != right.Length) {
return false
}
i := 1
while (i <= left.Length) {
if (left[i] != right[i]) {
return false
}
i += 1
}
return true
}
debugDebounce(params*) {
global appLastDebug
msg := joinToStr(params, quote := true)
if (msg != appLastDebug) {
OutputDebug(msg)
appLastDebug := msg
}
}
debugDebounceOneline(params*) {
global appLastDebug
msg := joinToStr(params, quote := true, oneline := true)
if (msg != appLastDebug) {
OutputDebug(msg)
appLastDebug := msg
}
}
debugLogDebounce(params*) {
msg := joinToStr(params)
if (appDebugLog.Length and appDebugLog[appDebugLog.Length] == msg) {
; debounce
return
}
appDebugLog.Push(msg)
if (appDebugLog.Length > MAX_DEBUG_LOG) {
appDebugLog.RemoveAt(1)
}
}
urlEncode(url) {
flags := 0x000C3000
cc := 4096
esc := ""
res := ""
E_POINTER := 0x80004003
loop {
VarSetStrCapacity(&esc, cc)
res := DllCall(
"Shlwapi.dll\UrlEscapeW",
"Str", url,
"Str", &esc,
"UIntP", &cc,
"UInt", flags,
"UInt")
} until (res != E_POINTER)
return esc
}
urlDecode(url) {
flags := 0x00140000
res := DllCall(
"Shlwapi.dll\UrlUnescape",
"Ptr", StrPtr(url),
"Ptr", 0,
"UInt", 0,
"UInt", flags,
"UInt")
if (res == 0) {
return ""
}
return url
}
getReadableKeyName(key) {
name := StrLower(key)
if (KEY_NAMES.Has(name)) {
return KEY_NAMES.Get(name)
}
else {
return name
}
}
getLocale() {
hwnd := WinExist("A")
if (not hwnd) {
return ""
}
threadId := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "Ptr", 0)
locale := DllCall("GetKeyboardLayout", "Ptr", threadId, "Ptr")
return locale
}
setLocale(locale) {
setLocaleAsync() {
hwnd := WinExist("A")
if (not hwnd) {
return ""
}
loop 10 {
if (getLocale() == locale) {
break
}
; Not always set from the first try
PostMessage(WM_INPUTLANGCHANGEREQUEST := 0x50, 0, locale, hwnd)
Sleep(100)
}
}
; Don't block current thread and make it interruptable, this can mess
; with the state and result in "alone" keys not correctly triggering
; after switching language since language switch keys are not correctly
; put in / removed from the appKeysPressed map.
SetTimer(setLocaleAsync, -10)
}
;; TODO: add in correct order (ex "m1" + "shift" before "m1")
addRemap(from, fromMods, to, toMods := []) {
config := Map(
"from_mods", fromMods,
"to", to,
"to_mods", toMods,
"options", [])
if (Type(from) == "Array") {
options := from.Clone()
from := options.RemoveAt(1)
config["options"] := options
}
if (appRemap.Has(from)) {
appRemap[from].Push(config)
}
else {
appRemap[from] := [config]
}
}
;; Receives the list of modifiers like ["m1", "shift"] and evaluates to
;; true if the corresponding keys are pressed
modsPressedForKey(mods, key) {
for i, modName in mods {
; used first to unset meta if it was set during initial keydown
if (modName == "always") {
return true
}
if (modName == "m1" or modName == "m2" or modName == "m3") {
if (not appMeta.Has(modName)) {
return false
}
if (not appMeta[modName]) {
return false
}
}
else if (modName == "alone") {
for curKey, keyInfo in appKeysPressed {
if (curKey != key) {
return false
}
else if (not keyInfo["alone"]) {
return false
}
}
}
; left and right controls are dual-mode from tab and enter, so treat
; them specially
else if (modName == "ctrl") {
if (key == "lctrl") {
; Prevent lctrl (tab) to reat it's own state as ctrl mod, rctrl
; (enter) is used to trigger ctrl-tab
if (not GetKeyState("rctrl", "P")) {
return false
}
}
else if (key == "rctrl") {
; Prevent rctrl (enter) to reat it's own state as ctrl mod, lctrl
; (tab) is used to trigger ctrl-enter
if (not GetKeyState("lctrl", "P")) {
return false
}
}
else {
if (not GetKeyState("ctrl", "P")) {
return false
}
}
}
else if (modName == "win") {
; lwin and rwin
if (not GetKeyState("vk5b", "P") and not GetKeyState("vk5c", "P")) {
return false
}
}
;; keys like "shift" etc
else if (not GetKeyState(modName, "P")) {
return false
}
}
return true
}
;; Receives the list of modifiers like ["ctrl", alt"] and evaluates to
;; an autohotkey modifiers string for send command, ex "^!"
modsToStr(mods) {
res := ""
for _, modName in mods {
if (modName == "win") {
res .= "#"
}
else if (modName == "ctrl") {
res .= "^"
}
else if (modName == "alt") {
res .= "!"
}
else if (modName == "shift") {
res .= "+"
}
else {
assert(false, "unknown mod in " . arrayToStr(mods))
}
}
return res
}
;; Given list of maps describing monitors evaluate to the index in that
;; list where the window specified by the description is located
monitorFromWnd(monitors, wnd) {
maxArea := 0
maxMonitorIdx := ""
for monitorIdx, monitor in monitors {
if (
wnd["right"] <= monitor["left"] or
wnd["left"] >= monitor["right"] or
wnd["top"] >= monitor["bottom"] or
wnd["bottom"] <= monitor["top"]
) {
continue
}
left := Max(wnd["left"], monitor["left"])
right := Min(wnd["right"], monitor["right"])
top := Max(wnd["top"], monitor["top"])
bottom := Min(wnd["bottom"], monitor["bottom"])
area := (right - left) * (bottom - top)
if (area > maxArea) {
maxArea := area
maxMonitorIdx := monitorIdx
}
}
return maxMonitorIdx
}
;; Takes map of "left", "right", "top", "bottom", "width", "height"
;; and recalculates specified attributes.
recalculateGeometry(geometry, attributes*) {
for _, attr in attributes {
if (attr == "left") {
geometry["left"] := geometry["right"] - geometry["width"]
}
if (attr == "right") {
geometry["right"] := geometry["left"] + geometry["width"]
}
else if (attr == "top") {
geometry["top"] := geometry["bottom"] - geometry["height"]
}
else if (attr == "bottom") {
geometry["bottom"] := geometry["top"] + geometry["height"]
}
else if (attr == "width") {
geometry["width"] := geometry["right"] - geometry["left"]
}
else if (attr == "height") {
geometry["height"] := geometry["bottom"] - geometry["top"]
}
}
}
moveActiveWnd(wndInfo) {
WinMove(
wndInfo["left"],
wndInfo["top"],
wndInfo["width"],
wndInfo["height"],
"A")
}
getCurWinPos() {
WinGetPos(&x, &y, &width, &height, "A")
return Map(
"left", x,
"right", x + width,
"top", y,
"bottom", y + height,
"width", width,
"height", height)
}
getMonitors() {
monitors := []
monitorCount := MonitorGetCount()
loop monitorCount {
MonitorGetWorkArea(a_index, &left, &top, &right, &bottom)
monitors.Push(Map(
"index", a_index,
"left", left,
"right", right,
"top", top,
"bottom", bottom,
"width", right - left,
"height", bottom - top
))
}
return monitors
}
setCurWinPos(pos) {
hwnd := WinExist("A")
if (not hwnd) {
return
}
wndInfo := getCurWinPos()
monitors := getMonitors()
monIdx := monitorFromWnd(monitors, wndInfo)
if (not monIdx) {
return
}
monInfo := monitors[monIdx]
if (pos == "left") {
wndInfo["left"] := monInfo["left"]
wndInfo["top"] := monInfo["top"]
wndInfo["width"] := monInfo["width"] / 2
wndInfo["height"] := monInfo["height"]
recalculateGeometry(wndInfo, "right", "bottom")
moveActiveWnd(wndInfo)
}
else if (pos == "right") {
wndInfo["right"] := monInfo["right"]
wndInfo["top"] := monInfo["top"]
wndInfo["width"] := monInfo["width"] / 2
wndInfo["height"] := monInfo["height"]
recalculateGeometry(wndInfo, "left", "bottom")
moveActiveWnd(wndInfo)
}
else if (pos == "top") {
wndInfo["left"] := monInfo["left"]
wndInfo["top"] := monInfo["top"]
wndInfo["width"] := monInfo["width"]
wndInfo["height"] := monInfo["height"] / 2
recalculateGeometry(wndInfo, "right", "bottom")
moveActiveWnd(wndInfo)
}
else if (pos == "bottom") {
wndInfo["left"] := monInfo["left"]
wndInfo["bottom"] := monInfo["bottom"]
wndInfo["width"] := monInfo["width"]
wndInfo["height"] := monInfo["height"] / 2
recalculateGeometry(wndInfo, "right", "top")
moveActiveWnd(wndInfo)
}
else if (pos == "topleft") {
wndInfo["left"] := monInfo["left"]
wndInfo["top"] := monInfo["top"]
wndInfo["width"] := monInfo["width"] / 2
wndInfo["height"] := monInfo["height"] / 2
recalculateGeometry(wndInfo, "right", "bottom")
moveActiveWnd(wndInfo)
}
else if (pos == "topright") {
wndInfo["right"] := monInfo["right"]
wndInfo["top"] := monInfo["top"]
wndInfo["width"] := monInfo["width"] / 2
wndInfo["height"] := monInfo["height"] / 2
recalculateGeometry(wndInfo, "left", "bottom")
moveActiveWnd(wndInfo)
}
else if (pos == "bottomleft") {
wndInfo["left"] := monInfo["left"]
wndInfo["bottom"] := monInfo["bottom"]
wndInfo["width"] := monInfo["width"] / 2
wndInfo["height"] := monInfo["height"] / 2
recalculateGeometry(wndInfo, "right", "top")
moveActiveWnd(wndInfo)
}
else if (pos == "bottomright") {
wndInfo["right"] := monInfo["right"]
wndInfo["bottom"] := monInfo["bottom"]
wndInfo["width"] := monInfo["width"] / 2
wndInfo["height"] := monInfo["height"] / 2
recalculateGeometry(wndInfo, "left", "top")
moveActiveWnd(wndInfo)
}
; Setting size instead of "maximizing" is better since "maximized"
; windows cannot be moved programmatically to the left and right
else if (pos == "max") {
wndInfo["left"] := monInfo["left"]
wndInfo["right"] := monInfo["right"]
wndInfo["top"] := monInfo["top"]
wndInfo["bottom"] := monInfo["bottom"]
recalculateGeometry(wndInfo, "width", "height")
moveActiveWnd(wndInfo)
}
else {
assert(false, "unknown pos " . pos)
}
}
setCurWinMon(dir) {
hwnd := WinExist("A")
if (not hwnd) {
return
}
wndInfo := getCurWinPos()
monitors := getMonitors()
monIdx := monitorFromWnd(monitors, wndInfo)
if (not monIdx) {
return
}
monInfo := monitors[monIdx]
dstMonIdx := ""
for curMonIdx, curMonInfo in monitors {
if (curMonIdx != monIdx) {
if (dir == "up") {
if (curMonInfo["bottom"] <= monInfo["top"]) {
dstMonIdx := curMonIdx
break
}
}
else if (dir == "down") {
if (curMonInfo["top"] >= monInfo["bottom"]) {
dstMonIdx := curMonIdx
break
}
}
else if (dir == "left") {
if (curMonInfo["right"] <= monInfo["left"]) {
dstMonIdx := curMonIdx
break
}
}
else if (dir == "right") {
if (curMonInfo["left"] >= monInfo["right"]) {
dstMonIdx := curMonIdx
break
}
}
}
}
if (not dstMonIdx) {
return
}
dstMonInfo := monitors[dstMonIdx]
; Scale window for the target monitor
dx := Abs(monInfo["left"] - wndInfo["left"]) / monInfo["width"]
dy := Abs(monInfo["top"] - wndInfo["top"]) / monInfo["height"]
dw := wndInfo["width"] / monInfo["width"]
dh := wndInfo["height"] / monInfo["height"]
wndInfo["left"] := dstMonInfo["left"] + dstMonInfo["width"] * dx
wndInfo["top"] := dstMonInfo["top"] + dstMonInfo["height"] * dy
wndInfo["width"] := dstMonInfo["width"] * dw
wndInfo["height"] := dstMonInfo["height"] * dh
recalculateGeometry(wndInfo, "right", "bottom")
; If window is bigger than display it will not be correctly placed
if (wndInfo["left"] < dstMonInfo["left"]) {
wndInfo["left"] := dstMonInfo["left"]
}
if (wndInfo["right"] > dstMonInfo["right"]) {
wndInfo["right"] := dstMonInfo["right"]
}
if (wndInfo["top"] < dstMonInfo["top"]) {
wndInfo["top"] := dstMonInfo["top"]
}
if (wndInfo["bottom"] > dstMonInfo["bottom"]) {
wndInfo["bottom"] := dstMonInfo["bottom"]
}
recalculateGeometry(wndInfo, "width", "height")
moveActiveWnd(wndInfo)
}
cycleCurWin(dir) {
hwnd := WinExist("A")
if (not hwnd) {
return
}
pid := WinGetPID("A")
process := WinGetProcessName("A")
windows := WinGetList()
sortArray(&windows)
activeWndFound := false
firstWnd := ""
prevWnd := ""
nextWnd := ""
lastWnd := ""
for win in windows {
if (WinGetProcessName(win) == process) {
if (not firstWnd) {
firstWnd := win
}
if (win == hwnd) {
activeWndFound := true
}
else {
if (not activeWndFound) {
prevWnd := win
}
else if(not nextWnd) {
nextWnd := win
}
}
lastWnd := win
}
}
; active wnd is first?
if (not prevWnd) {
prevWnd := lastWnd
}
; active wnd is last?
if (not nextWnd) {
nextWnd := firstWnd
}
if (dir == "prev") {
WinActivate(prevWnd)
}
else {
WinActivate(nextWnd)
}
}
switchToLang(lang) {
if (lang == "en") {
setLocale(0x04090409)
}
else if (lang == "ru") {
setLocale(0x04190419)
}
else if (lang == "jp") {
if (appLastLang == "jp") {
;; Switch between Hiragana and Latin input for Japanese keyboard
Send("!``")
}
else {
setLocale(0x04110411)
}
}
global appLastLang
appLastLang := lang
}
showSymbolPicker() {
wndInfo := getCurWinPos()
monitors := getMonitors()
monIdx := monitorFromWnd(monitors, wndInfo)
if (not monIdx) {
return
}
monInfo := monitors[monIdx]
wnd := Gui()
editBox := wnd.Add("Edit", "w300 h32 r1")
textBox := wnd.Add("Edit", "w300 h200 Disabled")
wnd.OnEvent("Escape", closeSymbolPicker)
wnd.OnEvent("Close", closeSymbolPicker)
symbolsByFilter(filter) {
filtered := []
for _, pair in appSymbols {
symbol := pair[1]
name := pair[2]
if (not filter or InStr(StrLower(name), StrLower(filter)) == 1) {
filtered.Push(pair)
}
}
return filtered
}
onUpdateSymbols(filter) {
filtered := symbolsByFilter(filter)
lines := []
for _, pair in filtered {
symbol := pair[1]
name := pair[2]
lines.Push(symbol . " " . name)
}
ControlSetText(joinWithSep("`r`n", lines), textBox)
}
onSymbolPickerKeydown(wParam, lParam, *) {
keyName := GetKeyName(Format('vk{:X}', wParam))
filter := ControlGetText(editBox)
if (keyName == "Enter") {
closeSymbolPicker()
pairs := symbolsByFilter(filter)
if (pairs) {
pair := pairs[1]
symbol := pair[1]
Send(symbol)
}
return
}
}
OnMessage(WM_KEYDOWN := 0x100, onSymbolPickerKeydown)
closeSymbolPicker(*) {
; OnMessage keeps a list of all registered functions
OnMessage(WM_KEYDOWN := 0x100, onSymbolPickerKeydown, 0)
OnMessage(WM_KEYUP := 0x101, onSymbolPickerKeyup, 0)
wnd.Destroy()
}
onSymbolPickerKeyup(wParam, lParam, *) {
keyName := GetKeyName(Format('vk{:X}', wParam))
filter := ControlGetText(editBox)
onUpdateSymbols(filter)
}
OnMessage(WM_KEYUP := 0x101, onSymbolPickerKeyup)
width := 320
height := 240
x := monInfo["left"] + (monInfo["width"] - width) / 2
y := monInfo["top"] + (monInfo["height"] - height) / 2
; Show initial unfiltered list of symbols
onUpdateSymbols("")
wnd.Show("x" . x . " y" . y . " w" . width . " h" . height)
}
onKeyCommand(items, dir) {
command := items.RemoveAt(1)
if (command == "nothing") {
return
}
if (command == "meta") {
name := items.RemoveAt(1)
if (dir == "down") {
appMeta[name] := true
}
else if (dir == "up") {
appMeta[name] := false
}
else {
assert(false, "unknown dir " . dir)
}
return
}
if (dir == "down") {
; Switch language on key down: since chord is very fast, switching it
; on key up will result in situations where modifier is released before
; the langauge switch key and key release will miss some modifier and
; will not be triggered.
if (command == "lang") {
lang := items.RemoveAt(1)
switchToLang(lang)
return
}
else if (command == "symbols") {
showSymbolPicker()
}
}
if (dir == "up") {
if (command == "winclose") {
hwnd := WinExist("A")
if (hwnd) {
if (WinGetTitle("A") == "Zoom Workplace") {
Run("taskkill /f /im zCefAgent.exe",, "Hide")
Run("taskkill /f /im Zoom.exe",, "Hide")
}
else {
winclose "A"
}
}
return
}
if (command == "delete") {
if (WinActive("ahk_exe explorer.exe")) {
;; Explorer monitors physical 'shift' key, so sending 'delete'
;; will trigger whift-delete, which is "permanently delete", while
;; ctrl-d key combination is just 'delte' in most apps.
Send("^d")
}
else {
Send("{delete}")
}
return
}
if (command == "send") {
input := items.RemoveAt(1)
Send(input)
return
}
if (command == "winpos") {
pos := items.RemoveAt(1)
setCurWinPos(pos)
return
}
if (command == "winmon") {
dir := items.RemoveAt(1)
setCurWinMon(dir)
return
}
if (command == "wincycle") {
dir := items.RemoveAt(1)
cycleCurWin(dir)
return
}
if (command == "winactivate") {
name := items.RemoveAt(1)
if (WinExist(name)) {
WinActivate(name)
}
return
}
if (command == "appactivate") {
name := items.RemoveAt(1)
if (WinExist("ahk_exe " . name)) {
WinActivate("ahk_exe " . name)
}
return
}
if (command == "lock") {
; Locking workstation prevents "key up" events and results in
; "stuck" meta.
appMeta.Clear()
DllCall("LockWorkStation")
return
}
if (command == "shorten") {
; Suppress linter error
clipboard := A_Clipboard
if (not RegExMatch(clipboard, "^https?://")) {
TrayTip("Error", "No URL in clipboard", ERR_ICON := 3)
SetTimer(TrayTip, ONCE_AFTER_MS := -2000)
return
}
if (RegExMatch(clipboard, "^https?://vk.cc")) {
TrayTip("Error", "Short URL in clipboard", ERR_ICON := 3)
SetTimer(TrayTip, ONCE_AFTER_MS := -2000)
return
}
if (RegExMatch(clipboard, "^https?://bit.ly")) {
TrayTip("Error", "Short URL in clipboard", ERR_ICON := 3)
SetTimer(TrayTip, ONCE_AFTER_MS := -2000)
return