-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLokiMaster.ahk
416 lines (405 loc) · 10.4 KB
/
LokiMaster.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
; ///////////////////////////////////////////
; An enhencement for Logi Master 3 like mouse
; Works with Logi Master 3 / Filco Minila air
; AutoHotkey 1.1.33.10 / Win11 21H2 22000.348
; ///////////////////////////////////////////
; ///////
; Compile
; ///////
;@Ahk2Exe-SetMainIcon %A_ScriptDir%/res/1/1.ico
; /////////
; Privilege
; /////////
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
; ////////////
; Environments
; ////////////
#NoEnv
;#Warn
#Persistent
#SingleInstance force
#UseHook
SendMode Input
SetWorkingDir %A_ScriptDir%
; ///////
; Globals
; ///////
global app_Name = "Loki Master" ; This is Used by Autorun & Msgbox
global app_Version = "0.21.1228"
global app_ConfigFile = "Config.ini"
global app_KeyDelay = 50
global app_TimerPeriod = 500
global desktop_Count = 1 ; Will be Updated by desktop_Update()
global desktop_Current = 1 ; Current desktop
global mode_User = 1 ; User Selected Mode
global mode_OnTheFly = 0 ; The Current Actived Mode by mode_WatchDog()
global mode_NameList := ["SMART","DESKTOP","CODE","OFFICE","BROWSER","MEDIA"] ; Mode Name List for OSD/Config
global mode_AppList := [] ; App Titles for Smart Mode
global mode_OSDList := [] ; Turn ON/OFF OSD with Specified Modes
; /////////
; Tray Menu
; /////////
Menu, Tray, NoStandard
Menu, Tray, Tip, %app_Name% %app_Version%
Menu, Tray, Add , Autorun, menuAutorun
Menu, Tray, Add , Suspend, menuSuspend
Menu, Tray, Add , Reload, menuReload
Menu, Tray, Add , Exit, menuExit
Menu, Tray, Default, Exit
; ////
; Main
; ////
SetKeyDelay, %app_KeyDelay%
config_Read()
autorun_Check()
SetTimer, mode_WatchDog, %app_TimerPeriod%
; /////
; Mouse
; /////
; Gestrue Key: Loop the Modes
XButton1:: ; Cycle Desktops
{
desktop_SwitchByCycle()
}
XButton2::Send #d
#Tab:: ; Switch Mode
If (mode_User < mode_NameList.Length()) {
mode_User++
}
Else {
mode_User = 1
}
config_Save()
; OutputDebug, [MODE] User: %mode_User%
; /////
; Modes
; /////
; Desktop
#If mode_OnTheFly = 2
WheelLeft::Volume_Up
WheelRight::Volume_Down
#If
; CODE
#If mode_OnTheFly = 3
; XButton1::Home
; XButton2::End
WheelLeft::Send {LCtrl Down}{NumpadAdd}{LCtrl Up} ; Zoom In
WheelRight::Send {LCtrl Down}{NumpadSub}{LCtrl Up} ; Zoom Out
#If
; Office
#If mode_OnTheFly = 4
; XButton1::Home
; XButton2::End
WheelLeft::Send {LCtrl Down}{WheelUp}{LCtrl Up} ; Zoom In
WheelRight::Send {LCtrl Down}{WheelDown}{LCtrl Up} ; Zoom Out
#If
; Browser
#If mode_OnTheFly = 5
; XButton1::Browser_Forward
; XButton2::Browser_Back
; MButton::Browser_Refresh
WheelLeft::Send {LCtrl Down}{NumpadAdd}{LCtrl Up} ; Zoom In
WheelRight::Send {LCtrl Down}{NumpadSub}{LCtrl Up} ; Zoom Out
#If
; Media
#If mode_OnTheFly = 6
LButton::Media_Prev
RButton::Media_Next
MButton::Media_Play_Pause
WheelLeft::Volume_Up
WheelRight::Volume_Down
#If
; ////////
; Keyboard
; ////////
; Capslock OSD
CapsLock:: invert_CapsLock()
; Desktop Control
+#1:: desktop_SwitchByNumber(1)
+#2:: desktop_SwitchByNumber(2)
+#3:: desktop_SwitchByNumber(3)
+#4:: desktop_SwitchByNumber(4)
+#5:: desktop_SwitchByNumber(5)
+#6:: desktop_SwitchByNumber(6)
+#7:: desktop_SwitchByNumber(7)
+#8:: desktop_SwitchByNumber(8)
+#9:: desktop_SwitchByNumber(9)
+#NumpadAdd:: desktop_Create()
+#NumpadSub:: desktop_Delete()
; Media Control
AppsKey & Up::Volume_Up
AppsKey & Down::Volume_Down
AppsKey & Left::Media_Prev
AppsKey & Right::Media_Next
AppsKey & Enter::Media_Play_Pause
; Swtich Appskey & RAlt
AppsKey::Ralt
RAlt::AppsKey
; /////////
; Menu Subs
; /////////
; Autorun
menuAutorun:
if autorun_Check() {
autorun_Disable() ; Check/Uncheck this Menu in autorun_Check()
}
Else {
autorun_Enable()
}
Return
; Suspend
menuSuspend:
if A_IsSuspended {
Suspend OFF
Menu, Tray, UnCheck, Suspend
}
Else {
Suspend On
Menu, Tray, Check, Suspend
}
Return
; Reload
menuReload:
Reload
Return
; Exit
menuExit:
MsgBox, 36, %app_Name%, Are you sure to Exit?
IfMsgBox Yes
ExitApp
Return
; /////////////
; Sub Functions
; /////////////
; Watchdog for Desktop & Mode
mode_WatchDog() {
static lastUserMode := 0, lastDesktop = 0
; Virtual Desktop
desktop_Update()
if (lastDesktop <> desktop_Current) {
lastDesktop := desktop_Current
RegRead, themeID, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize, SystemUsesLightTheme
if ErrorLevel {
themeID = 0 ; Default to dark
}
Menu, Tray, Icon, %A_ScriptDir%\res\%themeID%\%desktop_Current%.ico
; OutputDebug, [DESKTOP] OnTheFly: %mode_OnTheFly%
}
; Switch Mode
if (lastUserMode <> mode_User) {
lastUserMode := mode_User
mode_OnTheFly := mode_User
OSD(mode_NameList[(mode_User)])
}
; Smart Mode
if (mode_User = 1) {
dogCatched := False
winTitle = ""
WinGetActiveTitle, winTitle
if StrLen(winTitle) {
Loop % mode_NameList.Length() {
if winTitle Contains % mode_AppList[(A_Index)]
{
; New Mode Catched?
if (mode_OnTheFly <> A_Index) {
mode_OnTheFly := A_Index
; Check [OSD] Section in Config
if (mode_OSDList[(mode_OnTheFly)]) {
OSD(mode_NameList[(mode_OnTheFly)], True)
}
; OutputDebug, [MODE] OnTheFly: %mode_OnTheFly%
}
dogCatched := True
Break
}
}
}
; Fallback to Desktop Mode
if !dogCatched {
mode_OnTheFly := 2
}
}
Return
}
; Invert the CapsLock
invert_CapsLock() {
static flag_CpasLock := False
if flag_CpasLock {
SetCapsLockState, off
flag_CpasLock := False
OSD("Caps OFF")
}
else {
SetCapsLockState, on
flag_CpasLock := True
OSD("Caps ON")
}
Return
}
; On Screen Display
OSD(TXT, Grayed:=False)
{
backColor = EEAA99
Gui, OSD: +AlwaysOnTop +LastFound +Owner
Gui, OSD: Color, %backColor%
Gui, OSD: Font, s20, Verdana
if Grayed {
Gui, OSD: Add, Text, cSilver, %TXT%
}
Else {
Gui, OSD: Add, Text, cLime, %TXT%
}
WinSet, TransColor, %backColor% 200 ; Transparent
Gui, OSD: -Caption
Gui, OSD: Show, center center
Sleep, 500
Gui, OSD: destroy
return
}
; Update Virtual Desktops from Registry
desktop_Update() {
; Get the UUID of Current Desktop
IdLength = 32
RegRead, desktop_CurrentId, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, CurrentVirtualDesktop
if ErrorLevel {
OutputDebug, [DESKTOP] CurrentVirtualDesktop Failed
Return
}
if (desktop_CurrentId) {
IdLength := StrLen(desktop_CurrentId)
}
RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
if ErrorLevel {
OutputDebug, [DESKTOP] VirtualDesktopIDs Failed
Return
}
; Count Desktops
if DesktopList {
DesktopListLength := StrLen(DesktopList)
desktop_Count := Floor(DesktopListLength / IdLength)
}
else {
desktop_Count = 1
}
; Find the Current Desktop
while (desktop_CurrentId and A_Index <= desktop_Count) {
StartPos := ((A_Index-1) * IdLength) + 1
DesktopIter := SubStr(DesktopList, StartPos, IdLength)
if (DesktopIter = desktop_CurrentId) {
desktop_Current := A_Index
Break
}
}
}
; Cycle Desktops
desktop_SwitchByCycle() {
; Check Update before Switch
desktop_Update()
if (desktop_Current < desktop_Count)
{
Send ^#{Right}
desktop_Current++
}
else
{
While(desktop_Current>1)
{
Send ^#{Left}
desktop_Current--
}
}
; OutputDebug, [DESKTOP] Current: %desktop_Current%
}
; Swtich Desktops by Number
desktop_SwitchByNumber(desktop_Target)
{
desktop_Update()
; Check Invalid Numbers
if (desktop_Count < desktop_Target) {
desktop_Target := desktop_Count
}
; Swith to Target Desktop by Sending Keys
while (desktop_Current < desktop_Target) {
Send ^#{Right}
desktop_Current++
}
while (desktop_Current > desktop_Target) {
Send ^#{Left}
desktop_Current--
}
; OutputDebug, [DESKTOP] Current: %desktop_Current%
}
; Create & Switch to a New Virtual Desktop
desktop_Create()
{
Send, #^d
desktop_Count++
desktop_Current = %desktop_Count%
; OutputDebug, [DESKTOP] Created, Count: %desktop_Count%
}
; Delete the Current Virtual Desktop
desktop_Delete()
{
Send, #^{F4}
desktop_Count--
desktop_Current--
; OutputDebug, [DESKTOP] Deleted, Count: %desktop_Count%
}
; Enable Autorun
autorun_Enable() {
RegWrite, REG_SZ, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Run, %app_Name%, %A_ScriptFullPath%
if autorun_Check() {
Return True
}
Return False
}
; Disable Autorun
autorun_Disable() {
RegDelete, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run, %app_Name%
if autorun_Check() {
Return False
}
Return True
}
; Check Autorun Status
autorun_Check() {
RegRead, testPath, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Run, %app_Name%
if (testPath = A_ScriptFullPath) {
Menu, Tray, Check, Autorun
Return True
}
Menu, Tray, UnCheck, Autorun
Return False
}
; Save Configs
config_Save() {
IniWrite, %mode_User%, %app_ConfigFile%, APP, MODE_USER
Return
}
; Read Configs
config_Read() {
IniRead, mode_User, %app_ConfigFile%, APP, MODE_USER, 1
IniRead, app_KeyDelay, %app_ConfigFile%, APP, KEY_DELAY, 50
IniRead, app_TimerPeriod, %app_ConfigFile%, APP, TIMER_PERIOD, 500
Loop % mode_NameList.Length() {
strApp := ""
strOSD := False
strKey := mode_NameList[(A_Index)]
IniRead, strApp, %app_ConfigFile%, MODE, %strKey%, ""
IniRead, strOSD, %app_ConfigFile%, SMART_OSD, %strKey%, False
mode_AppList[(A_Index)] := strApp
mode_OSDList[(A_Index)] := strOSD
}
Return
}