-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyToggles.ahk
604 lines (483 loc) · 14.5 KB
/
KeyToggles.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
; KeyToggles v1.5
; TODO
; add application profiles (https://stackoverflow.com/questions/45190170/how-can-i-make-this-ini-file-into-a-listview-in-autohotkey)
; add overlay
; merge similar functions
#MaxThreadsPerHotkey 1 ; Prevent accidental double-presses.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent ; Keep the script permanently running since we use a timer.
#Requires AutoHotkey v1.1.33.02+ ; Display an error and quit if this version requirement is not met.
#SingleInstance force ; Allow only a single instance of the script to run.
#UseHook ; Allow listening for non-modifier keys.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; Register a function to be called on exit
OnExit("ExitFunc")
; Constants
KEY_MODE_TOGGLE := 1
KEY_MODE_HOLD := 2
KEY_MODE_AUTOFIRE := 3
; Initialize state variables
bAiming := false
bCrouching := false
bSprinting := false
bAutofireAiming := false
bAutofireCrouching := false
bAutofireSprinting := false
bRestoreAiming := false
bRestoreCrouching := false
bRestoreSprinting := false
bToggleKeysSnapshotTaken := false
windowID := 0
; A handy label to allow quickly jumping back to top in AHK Studio
init:
ReadConfigFile()
RestartAsAdminIfNeeded()
SetTimer, OnFocusChanged, %nFocusCheckDelay%
return
aimLabel:
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% begin
switch bAimMode
{
case KEY_MODE_TOGGLE:
isMouseButton := IsMouseButton(A_ThisHotkey)
isMouseOverWindow := IsMouseOverWindow(windowID)
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% isMouseButton(%isMouseButton%) isMouseOverWindow(%isMouseOverWindow%)
; Fixes an issue where you couldn't click outside the window if the toggle key was a mouse button and was enabled
if (isMouseButton && !isMouseOverWindow)
{
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% outside window
SendClick(A_ThisHotkey)
}
; Otherwise toggle the key
else
AimToggle(!bAiming, true)
case KEY_MODE_HOLD:
AimHold()
case KEY_MODE_AUTOFIRE:
; Based on https://autohotkey.com/board/topic/64576-the-definitive-autofire-thread/?p=407264
bAutofireAiming := !bAutofireAiming
SetTimer, AimAutofire, % bAutofireAiming ? nAutofireKeyDelay : "Off"
KeyWait, %aimAutofireKey%
}
return
crouchLabel:
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% begin
switch bCrouchMode
{
case KEY_MODE_TOGGLE:
isMouseButton := IsMouseButton(A_ThisHotkey)
isMouseOverWindow := IsMouseOverWindow(windowID)
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% isMouseButton(%isMouseButton%) isMouseOverWindow(%isMouseOverWindow%)
if (isMouseButton && !isMouseOverWindow)
{
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% outside window
SendClick(A_ThisHotkey)
}
else
CrouchToggle(!bCrouching, true)
case KEY_MODE_HOLD:
CrouchHold()
case KEY_MODE_AUTOFIRE:
bAutofireCrouching := !bAutofireCrouching
SetTimer, CrouchAutofire, % bAutofireCrouching ? nAutofireKeyDelay : "Off"
KeyWait, %crouchAutofireKey%
}
return
sprintLabel:
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% begin
switch bSprintMode
{
case KEY_MODE_TOGGLE:
isMouseButton := IsMouseButton(A_ThisHotkey)
isMouseOverWindow := IsMouseOverWindow(windowID)
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% isMouseButton(%isMouseButton%) isMouseOverWindow(%isMouseOverWindow%)
if (isMouseButton && !isMouseOverWindow)
{
;OutputDebug, %A_ThisLabel%::%A_ThisHotkey% outside window
SendClick(A_ThisHotkey)
}
else
SprintToggle(!bSprinting, true)
case KEY_MODE_HOLD:
SprintHold()
case KEY_MODE_AUTOFIRE:
bAutofireSprinting := !bAutofireSprinting
SetTimer, SprintAutofire, % bAutofireSprinting ? nAutofireKeyDelay : "Off"
KeyWait, %sprintAutofireKey%
}
return
AimAutofire()
{
global
;OutputDebug, %A_ThisFunc%::begin
SendKey(aimKey, nKeyDelay)
;OutputDebug, %A_ThisFunc%::end
}
AimHold()
{
global
;OutputDebug, %A_ThisFunc%::begin
SendKey(aimKey, nKeyDelay)
KeyWait, %aimKey%
SendKey(aimKey, nKeyDelay)
;OutputDebug, %A_ThisFunc%::end
}
AimToggle(pAiming, pWait := false)
{
global
;OutputDebug, %A_ThisFunc%::begin
bAiming := pAiming
;OutputDebug, %A_ThisFunc%::bAiming(%bAiming%)
SendInput % bAiming ? "{" . aimKey . " down}" : "{" . aimKey . " up}"
if (pWait)
KeyWait, %aimKey%
;OutputDebug, %A_ThisFunc%::end
}
CrouchAutofire()
{
global
;OutputDebug, %A_ThisFunc%::begin
SendKey(crouchKey, nKeyDelay)
;OutputDebug, %A_ThisFunc%::end
}
CrouchHold()
{
global
;OutputDebug, %A_ThisFunc%::begin
SendKey(crouchKey, nKeyDelay)
KeyWait, %crouchKey%
SendKey(crouchKey, nKeyDelay)
;OutputDebug, %A_ThisFunc%::end
}
CrouchToggle(pCrouching, pWait := false)
{
global
;OutputDebug, %A_ThisFunc%::begin
bCrouching := pCrouching
;OutputDebug, %A_ThisFunc%::bCrouching(%bCrouching%)
SendInput % bCrouching ? "{" . crouchKey . " down}" : "{" . crouchKey . " up}"
if (pWait)
KeyWait, %crouchKey%
;OutputDebug, %A_ThisFunc%::end
}
HookWindow()
{
; All the variables below are declared as global so they can be used in the whole script
global
; Make the hotkeys active only for a specific window
WinGet, windowID, ID, %sWindowName%
OutputDebug, %A_ThisFunc%::WinGet(%windowID%)
GroupAdd, windowIDGroup, ahk_id %windowID%
Hotkey, IfWinActive, ahk_group windowIDGroup
if (windowID && bShowNotifications)
TrayTip, %configFileNameTrimmed%, % "The window """ . sWindowName . """ has been hooked."
}
IsMouseButton(pKey)
{
mouseButtonsList := "LButton MButton RButton XButton1 XButton2"
return InStr(mouseButtonsList, pKey) != false
}
IsMouseOver(pWinTitle)
{
MouseGetPos, , , winID
return WinExist(pWinTitle . " ahk_id " . winID)
}
IsMouseOverWindow(pHwnd)
{
MouseGetPos, , , mouseWindowID
return pHwnd == mouseWindowID
}
; Hook the window and register hotkeys if necessary, disable toggles on focus lost and optionally restore them on focus
OnFocusChanged()
{
global
OutputDebug, %A_ThisFunc%::WinWaitActive
WinWaitActive, %sWindowName%
Sleep, %nHookDelay%
; Make sure to hook the window again if it no longer exists
if (windowID != WinExist(sWindowName))
{
HookWindow()
RegisterHotkeys()
; That's a different window, don't restore toggle states
bRestoreAiming := false
bRestoreCrouching := false
bRestoreSprinting := false
}
; Restore toggle states
if (ShouldRestoreTogglesOnFocus())
{
OutputDebug, %A_ThisFunc%::restoreToggleStates(%bRestoreAiming%, %bRestoreCrouching%, %bRestoreSprinting%)
if (bRestoreAiming)
AimToggle(true)
if (bRestoreCrouching)
CrouchToggle(true)
if (bRestoreSprinting)
SprintToggle(true)
}
OutputDebug, %A_ThisFunc%::WinWaitNotActive
WinWaitNotActive, %sWindowName%
; Save toggle states
if (ShouldRestoreTogglesOnFocus())
{
; A snapshot of the toggle states was already taken elsewhere, don't take another one
if (bToggleKeysSnapshotTaken)
bToggleKeysSnapshotTaken := false
else
{
OutputDebug, %A_ThisFunc%::saveToggleStates(%bRestoreAiming%, %bRestoreCrouching%, %bRestoreSprinting%)
bRestoreAiming := bAiming
bRestoreCrouching := bCrouching
bRestoreSprinting := bSprinting
}
}
ReleaseAllKeys()
}
ReadConfigFile()
{
; All the variables below are declared as global so they can be used in the whole script
global
SplitPath, A_ScriptName, , , , configFileNameTrimmed
configFileName := configFileNameTrimmed . ".ini"
; Config file is missing, exit
if (!FileExist(configFileName))
ExitWithErrorMessage(configFileName . " not found! The script will now exit.")
; General
IniRead, sWindowName, %configFileName%, General, windowName, "put_window_name_here"
IniRead, bAimMode, %configFileName%, General, aimMode, 1
IniRead, bCrouchMode, %configFileName%, General, crouchMode, 1
IniRead, bSprintMode, %configFileName%, General, sprintMode, 1
IniRead, nAutofireKeyDelay, %configFileName%, General, autofireKeyDelay, 100
IniRead, bFixSystemKeys, %configFileName%, General, fixSystemKeys, 1
IniRead, nFocusCheckDelay, %configFileName%, General, focusCheckDelay, 1000
IniRead, nHookDelay, %configFileName%, General, hookDelay, 0
IniRead, nKeyDelay, %configFileName%, General, keyDelay, 0
IniRead, bRestoreTogglesOnFocus, %configFileName%, General, restoreTogglesOnFocus, 0
IniRead, bShowNotifications, %configFileName%, General, showNotifications, 0
IniRead, bRunAsAdmin, %configFileName%, General, runAsAdmin, 0
; Keys
IniRead, aimKey, %configFileName%, Keys, aimKey, RButton
IniRead, crouchKey, %configFileName%, Keys, crouchKey, LCtrl
IniRead, sprintKey, %configFileName%, Keys, sprintKey, LShift
IniRead, aimAutofireKey, %configFileName%, Keys, aimAutofireKey, F1
IniRead, crouchAutofireKey, %configFileName%, Keys, crouchAutofireKey, F2
IniRead, sprintAutofireKey, %configFileName%, Keys, sprintAutofireKey, F3
; Debug
IniRead, bDebugMode, %configFileName%, Debug, debugMode, 0
if (sWindowName == "put_window_name_here")
ExitWithErrorMessage("You must specify a window name! The script will now exit.")
}
RegisterHotkeys()
{
global
; Enabled only for toggle and hold modes
Hotkey, %aimKey%, aimLabel, % bAimMode == KEY_MODE_TOGGLE || bAimMode == KEY_MODE_HOLD ? "On" : "Off"
Hotkey, %crouchKey%, crouchLabel, % bAimMode == KEY_MODE_TOGGLE || bAimMode == KEY_MODE_HOLD ? "On" : "Off"
Hotkey, %sprintKey%, sprintLabel, % bAimMode == KEY_MODE_TOGGLE || bAimMode == KEY_MODE_HOLD ? "On" : "Off"
; Enabled only for autofire mode
Hotkey, %aimAutofireKey%, aimLabel, % bAimMode == KEY_MODE_AUTOFIRE ? "On" : "Off"
Hotkey, %crouchAutofireKey%, crouchLabel, % bCrouchMode == KEY_MODE_AUTOFIRE ? "On" : "Off"
Hotkey, %sprintAutofireKey%, sprintLabel, % bSprintMode == KEY_MODE_AUTOFIRE ? "On" : "Off"
; Fixes issues when pressing system keys while toggle keys are modifiers and are enabled
Hotkey, !Tab, SendAltTab, % bFixSystemKeys ? "On" : "Off"
Hotkey, Escape, SendEscape, % bFixSystemKeys ? "On" : "Off"
Hotkey, LWin, SendWindows, % bFixSystemKeys ? "On" : "Off"
Hotkey, RWin, SendWindows, % bFixSystemKeys ? "On" : "Off"
}
ReleaseAllKeys()
{
global
OutputDebug, %A_ThisFunc%::states(%bAiming%, %bCrouching%, %bSprinting%)
if (bAiming)
AimToggle(false)
if (bCrouching)
CrouchToggle(false)
if (bSprinting)
SprintToggle(false)
bAutofireAiming := false
bAutofireCrouching := false
bAutofireSprinting := false
SetTimer, AimAutofire, Off
SetTimer, CrouchAutofire, Off
SetTimer, SprintAutofire, Off
}
RestartAsAdminIfNeeded()
{
global
; Restart the script as admin
if (bRunAsAdmin && !A_IsAdmin)
{
try
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
ExitApp
}
}
}
SendAltTab()
{
;OutputDebug, %A_ThisFunc%::begin
; Take a snapshot of the toggle states
TakeToggleKeysSnapshot()
; Check if modifier keys are physically pressed to handle Ctrl+Alt+Tab, Shift+Alt+Tab and Ctrl+Shift+Alt+Tab
if (GetKeyState("Control", "P"))
SendInput {Control down}
if (GetKeyState("Shift", "P"))
SendInput {Shift down}
SendInput {Alt down}{Tab}
;OutputDebug, %A_ThisFunc%::end
}
SendClick(pKey)
{
;OutputDebug, %A_ThisFunc%::begin
; Take a snapshot of the toggle states
TakeToggleKeysSnapshot()
SendKey(pKey, 0, true)
;OutputDebug, %A_ThisFunc%::end
}
SendEscape()
{
;OutputDebug, %A_ThisFunc%::begin
; Take a snapshot of the toggle states
TakeToggleKeysSnapshot()
; Check if modifier keys are physically pressed to handle Ctrl+Escape and Ctrl+Shift+Escape
if (GetKeyState("Control", "P"))
SendInput {Control down}
if (GetKeyState("Shift", "P"))
SendInput {Shift down}
SendInput {Escape}
; Fixes an issue where the window wouldn't receive key up events when pressing Ctrl+Shift+Escape
ControlSend, ahk_parent, {Control up}{Shift up}
;OutputDebug, %A_ThisFunc%::end
}
SendKey(pKey, pSleepMs := 0, pWait := false)
{
SendInput % "{" . pKey . " down}"
if (pSleepMs > 0)
Sleep, %pSleepMs%
if (pWait)
KeyWait, %pKey%
SendInput % "{" . pKey . " up}"
}
SendWindows()
{
;OutputDebug, %A_ThisFunc%::begin
; Take a snapshot of the toggle states
TakeToggleKeysSnapshot()
; Check if modifier keys are physically pressed to handle Shift+Win
if (GetKeyState("Shift", "P"))
SendInput {Shift down}
SendInput {LWin}
;OutputDebug, %A_ThisFunc%::end
}
ShouldRestoreTogglesOnFocus()
{
global
return bRestoreTogglesOnFocus && bAimMode == KEY_MODE_TOGGLE || bCrouchMode == KEY_MODE_TOGGLE || bSprintMode == KEY_MODE_TOGGLE && WinExist(sWindowName)
}
SprintAutofire()
{
global
;OutputDebug, %A_ThisFunc%::begin
SendKey(sprintKey, nKeyDelay)
;OutputDebug, %A_ThisFunc%::end
}
SprintHold()
{
global
;OutputDebug, %A_ThisFunc%::begin
SendKey(sprintKey, nKeyDelay)
KeyWait, %sprintKey%
SendKey(sprintKey, nKeyDelay)
;OutputDebug, %A_ThisFunc%::end
}
SprintToggle(pSprinting, pWait := false)
{
global
;OutputDebug, %A_ThisFunc%::begin
bSprinting := pSprinting
;OutputDebug, %A_ThisFunc%::bSprinting(%bSprinting%)
SendInput % bSprinting ? "{" . sprintKey . " down}" : "{" . sprintKey . " up}"
if (pWait)
KeyWait, %sprintKey%
;OutputDebug, %A_ThisFunc%::end
}
TakeToggleKeysSnapshot(pReleaseKeys := true)
{
global
if (ShouldRestoreTogglesOnFocus())
{
bRestoreAiming := bAiming
bRestoreCrouching := bCrouching
bRestoreSprinting := bSprinting
bToggleKeysSnapshotTaken := true
}
if (pReleaseKeys)
ReleaseAllKeys()
}
; Exit script
ExitFunc(pExitReason, pExitCode)
{
;OutputDebug, %A_ThisFunc%::pExitReason(%pExitReason%) pExitCode(%pExitCode%)
; Only release keys if the script is closed from the tray menu or reloaded/replaced
if (pExitReason != "Exit")
ReleaseAllKeys()
}
; Display an error message and exit
ExitWithErrorMessage(pErrorMessage)
{
MsgBox, 16, Error, %pErrorMessage%
ExitApp, 1
}
; Fixes an issue where you couldn't click outside the window while toggle keys are mouse buttons and are enabled
#IfWinActive ahk_group windowIDGroup
LButton::
MButton::
RButton::
XButton1::
XButton2::
if (!IsMouseOverWindow(windowID))
{
;OutputDebug, %A_ThisHotkey%::outside window
SendClick(A_ThisHotkey)
}
else
{
;OutputDebug, %A_ThisHotkey%::inside window
SendKey(A_ThisHotkey, 0, true)
}
return
#IfWinActive
#If bDebugMode
; Exit script
!F10:: ; ALT+F10
Suspend, Permit
ExitApp
return
; Reload script
!F11:: ; ALT+F11
Suspend, Permit
Reload
return
#If
; Suspend script (useful when in menus)
!F12:: ; ALT+F12
Suspend
; Single beep when suspended
if (A_IsSuspended)
{
SoundBeep, 1000
ReleaseAllKeys()
}
; Double beep when resumed
else
{
SoundBeep, 1000
SoundBeep, 1000
}
return