77 lines
2.8 KiB
AutoHotkey
77 lines
2.8 KiB
AutoHotkey
|
; === CONFIGURATION (Filled by Python) ===
|
||
|
TargetWindowTitle := "{TARGET_WINDOW_TITLE_PLACEHOLDER}"
|
||
|
KeyToPressAHK := "{KEY_TO_PRESS_AHK_PLACEHOLDER}"
|
||
|
StopKeyAHK := "{STOP_KEY_AHK_PLACEHOLDER}"
|
||
|
AHKLogFile := "{AHK_LOG_FILE_PLACEHOLDER}"
|
||
|
|
||
|
; === AHK LOGGING FUNCTION ===
|
||
|
Log(message) {{
|
||
|
FileAppend(message "`n", AHKLogFile)
|
||
|
}}
|
||
|
|
||
|
; === SCRIPT LOGIC ===
|
||
|
|
||
|
Log("AHK Script started.")
|
||
|
; Global state variable to track if the key is currently pressed down
|
||
|
isKeyDown := false
|
||
|
|
||
|
{STOP_KEY_AHK_PLACEHOLDER}::
|
||
|
{{
|
||
|
Log("Stop key pressed. Releasing key and exiting.")
|
||
|
Send("{{KeyToPressAHK Up}}") ; Ensure key is released on manual stop
|
||
|
ExitApp
|
||
|
}}
|
||
|
|
||
|
SetTimer(() => SendKeyLoop(), 100)
|
||
|
|
||
|
SendKeyLoop() {{
|
||
|
global isKeyDown
|
||
|
|
||
|
; Check if window exists and is NOT active, then attempt activation
|
||
|
If WinExist(TargetWindowTitle) && not WinActive(TargetWindowTitle) {{
|
||
|
Log("Target window '" TargetWindowTitle "' exists but not active. Attempting activation.")
|
||
|
WinActivate(TargetWindowTitle)
|
||
|
Sleep 50 ; Give a moment for the window to become active
|
||
|
if WinActive(TargetWindowTitle) {{
|
||
|
Log("Target window '" TargetWindowTitle "' is now active.")
|
||
|
}}
|
||
|
}}
|
||
|
; If window does NOT exist
|
||
|
Else {{
|
||
|
Log("Target window '" TargetWindowTitle "' not found. Stopping script.")
|
||
|
If (isKeyDown) {{
|
||
|
Send("{{KeyToPressAHK Up}}")
|
||
|
isKeyDown := false
|
||
|
Log("Key released (Target window not found).")
|
||
|
}}
|
||
|
ExitApp ; <<< NEW: Exit AHK script if window is not found
|
||
|
}}
|
||
|
|
||
|
; After potential activation attempt, or if already active, check active state to send/release key
|
||
|
if WinActive(TargetWindowTitle) {{
|
||
|
If (not isKeyDown) {{ ; Only press down if not already down
|
||
|
Send("{{KeyToPressAHK Down}}")
|
||
|
isKeyDown := true
|
||
|
Log("Key pressed down (Target window active).")
|
||
|
}}
|
||
|
}} Else {{
|
||
|
; Window exists but is not active (either initially not active, or activation failed)
|
||
|
Log("Target window '" TargetWindowTitle "' exists but failed to activate.")
|
||
|
If (isKeyDown) {{
|
||
|
Send("{{KeyToPressAHK Up}}")
|
||
|
isKeyDown := false
|
||
|
Log("Key released (Failed to activate target window).")
|
||
|
}}
|
||
|
}}
|
||
|
}}
|
||
|
|
||
|
OnScriptExit(*) {{ ; Defined as a function that accepts any number of parameters
|
||
|
global isKeyDown
|
||
|
If (isKeyDown) {{ ; Ensure key is released if script exits while it's down
|
||
|
Send("{{KeyToPressAHK Up}}")
|
||
|
Log("Key released (Script exiting).")
|
||
|
}}
|
||
|
Log("AHK Script exiting.")
|
||
|
ExitApp
|
||
|
}}
|