Why does hotkey with Reload command break code execution?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
hced
Posts: 9
Joined: 11 Nov 2019, 13:30

Why does hotkey with Reload command break code execution?

22 Aug 2020, 18:13

Why does this display the MsgBox :

Code: Select all

; -------------------------
; _INIT.ahk - Master script
; -------------------------

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases

SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory
#Include %A_ScriptDir%  	; Changes the working directory for subsequent #Includes and FileInstalls.
							; (Must have this in order for #Include calls to work)

MsgBox This will be displayed

; Shortcut: Reload this script (Ctrl + Win + Alt + R)
^#!r::
    ; Send, ^s ; To save a changed script
    ; Sleep, 300 ; give it time to save the script
    Reload

; Confirm once _INIT.ahk has been (re)loaded
TrayTip, AutoHotKey, _INIT.ahk has been (re)run, 1, 16
SoundPlay, Assets\Sounds\UI-Sound-01.wav

return ; <-- Not sure if needed (I'm a nuub)
But this won't :

Code: Select all

; -------------------------
; _INIT.ahk - Master script
; -------------------------

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases

SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory
#Include %A_ScriptDir%  	; Changes the working directory for subsequent #Includes and FileInstalls.
							; (Must have this in order for #Include calls to work)

; Shortcut: Reload this script (Ctrl + Win + Alt + R)
^#!r::
    ; Send, ^s ; To save a changed script
    ; Sleep, 300 ; give it time to save the script
    Reload

MsgBox This won't be displayed

; Confirm once _INIT.ahk has been (re)loaded
TrayTip, AutoHotKey, _INIT.ahk has been (re)run, 1, 16
SoundPlay, Assets\Sounds\UI-Sound-01.wav

return ; <-- Not sure if needed (I'm a nuub)
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: Why does hotkey with Reload command break code execution?

22 Aug 2020, 18:35

At script start, code execution will start at the top of the script until the end of the auto-execute section is reached - that means, it will continue until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).
So, it's the existence of a hotkey definition, it has nothing to do with the Reload statement in it.

That makes the msgbox line in your second snippet unreachable - but also the lines TrayTip, AutoHotKey, _INIT.ahk has been (re)run, 1, 16 and
SoundPlay, Assets\Sounds\UI-Sound-01.wav in your first (and second) code snippet....

(Of course, you can use control flow statements - for example, to execute code conditionally: https://www.autohotkey.com/docs/Concepts.htm#control-flow)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], bobstoner289, peter_ahk, Spawnova and 352 guests