Skip to content

Commit

Permalink
- major code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GenesisFR committed May 2, 2021
1 parent 08246b8 commit 778f487
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 58 deletions.
131 changes: 77 additions & 54 deletions KeyToggles.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,9 @@ configFileName := RTrim(A_ScriptName, A_IsCompiled ? ".exe" : ".ahk") . ".ini"
if (!FileExist(configFileName))
ExitWithErrorMessage(configFileName . " not found! The script will now exit.")

; Read options from config file
IniRead, windowName, %configFileName%, General, windowName
IniRead, isAimToggle, %configFileName%, General, isAimToggle, 1
IniRead, isCrouchToggle, %configFileName%, General, isCrouchToggle, 1
IniRead, isSprintToggle, %configFileName%, General, isSprintToggle, 1
IniRead, hookDelay, %configFileName%, General, hookDelay, 0
IniRead, restoreTogglesOnFocus, %configFileName%, General, restoreTogglesOnFocus, 1
IniRead, aimKey, %configFileName%, Keys, aimKey, RButton
IniRead, crouchKey, %configFileName%, Keys, crouchKey, LCtrl
IniRead, sprintKey, %configFileName%, Keys, sprintKey, LShift
IniRead, isDebug, %configFileName%, Debug, isDebug, 0

; Make the hotkeys active only for a specific window
WinWaitActive, %windowName%
Sleep, %hookDelay%
WinGet, windowID, ID, %windowName%
GroupAdd, windowIDGroup, ahk_id %windowID%
Hotkey, IfWinActive, ahk_group windowIDGroup

if (isAimToggle)
Hotkey, %aimKey%, aimLabel
if (isCrouchToggle)
Hotkey, %crouchKey%, crouchLabel
if (isSprintToggle)
Hotkey, %sprintKey%, sprintLabel
ReadConfigFile()
HookWindow()
RegisterHotkeys()

SetTimer, SetTogglesOnFocus, 1000
return
Expand All @@ -67,14 +45,11 @@ return

; Disable toggles on focus lost and restore them on focus
SetTogglesOnFocus:
If WinActive(windowName)
if WinActive(windowName)
{
WinWaitNotActive, %windowName%

; Save toggle states
global isAiming
global isCrouching
global isSprinting
tempIsAiming := isAiming
tempIsCrouching := isCrouching
tempIsSprinting := isSprinting
Expand All @@ -96,36 +71,98 @@ return
; Toggle aim
Aim(ByRef pIsAiming)
{
global isAiming := pIsAiming
global aimKey
global

isAiming := pIsAiming
SendInput % isAiming ? "{" . aimKey . " down}" : "{" . aimKey . " up}"
KeyWait, %aimKey%
}

; Toggle crouch
Crouch(ByRef pIsCrouching)
{
global isCrouching := pIsCrouching
global crouchKey
global

isCrouching := pIsCrouching
SendInput % isCrouching ? "{" . crouchKey . " down}" : "{" . crouchKey . " up}"
KeyWait, %crouchKey%
}

; Disable all toggles
DisableAllToggles()
{
Aim(false)
Crouch(false)
Sprint(false)
}

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
WinWaitActive, %windowName%
Sleep, %hookDelay%
WinGet, windowID, ID, %windowName%
GroupAdd, windowIDGroup, ahk_id %windowID%
Hotkey, IfWinActive, ahk_group windowIDGroup
}

ReadConfigFile()
{
; All the variables below are declared as global so they can be used in the whole script
global

; General
IniRead, windowName, %configFileName%, General, windowName
IniRead, bAimToggle, %configFileName%, General, bAimToggle, 1
IniRead, bCrouchToggle, %configFileName%, General, bCrouchToggle, 1
IniRead, bSprintToggle, %configFileName%, General, bSprintToggle, 1
IniRead, hookDelay, %configFileName%, General, hookDelay, 0
IniRead, restoreTogglesOnFocus, %configFileName%, General, restoreTogglesOnFocus, 1

; Keys
IniRead, aimKey, %configFileName%, Keys, aimKey, RButton
IniRead, crouchKey, %configFileName%, Keys, crouchKey, LCtrl
IniRead, sprintKey, %configFileName%, Keys, sprintKey, LShift

; Debug
IniRead, bDebug, %configFileName%, Debug, bDebug, 0
}

RegisterHotkeys()
{
; All the variables below are declared as global so they can be used in the whole script
global

Hotkey, %aimKey%, aimLabel, % bAimToggle ? "On" : "Off"
Hotkey, %crouchKey%, crouchLabel, % bCrouchToggle ? "On" : "Off"
Hotkey, %sprintKey%, sprintLabel, % bSprintToggle ? "On" : "Off"
}

; Toggle sprint
Sprint(ByRef pIsSprinting)
{
global isSprinting := pIsSprinting
global sprintKey
global

isSprinting := pIsSprinting
SendInput % isSprinting ? "{" . sprintKey . " down}" : "{" . sprintKey . " up}"
KeyWait, %sprintKey%
}

; Disable all toggles
DisableAllToggles()
; Exit script
ExitFunc(ExitReason, ExitCode)
{
Aim(false)
Crouch(false)
Sprint(false)
DisableAllToggles()
ExitApp
}

; Display an error message and exit
ExitWithErrorMessage(ErrorMessage)
{
MsgBox, 16, Error, %ErrorMessage%
ExitApp, -1
}

; Suspend script (useful when in menus)
Expand All @@ -144,17 +181,3 @@ else

DisableAllToggles()
return

; Display an error message and exit
ExitWithErrorMessage(ErrorMessage)
{
MsgBox, 16, Error, %ErrorMessage%
ExitApp, -1
}

; Exit script
ExitFunc(ExitReason, ExitCode)
{
DisableAllToggles()
ExitApp
}
8 changes: 4 additions & 4 deletions KeyToggles.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
windowName="put_window_name_here"

; Set these to 1 to enable toggles individually (0 to disable)
isAimToggle=1
isCrouchToggle=1
isSprintToggle=1
bAimToggle=1
bCrouchToggle=1
bSprintToggle=1

; How much time to wait (in milliseconds) before hooking the window
; Increase this delay in case toggles don't work in some games
Expand All @@ -36,5 +36,5 @@ sprintKey=LShift

[Debug]
; Display information for debug purposes
isDebug=0
bDebug=0

0 comments on commit 778f487

Please sign in to comment.