#include causing the code from the first hotkey label to run on load.

Report problems with documented functionality
User avatar
GollyJer
Posts: 69
Joined: 19 Sep 2015, 19:33
Contact:

#include causing the code from the first hotkey label to run on load.

12 Sep 2016, 15:50

I came up with a way (code shown below) to allow AutoExec behavior within #Include scripts even if they have hotkeys defined.
The technique actually works as expected. If you create the three files and one directory described below and run IncludeTest.ahk, you will see one message box.
Image

However, if you uncomment the line
^1:: MsgBox "Hello from Include1"
in Include1.akh and rerun IncludeTest.ahk then everything loads just like before, but the code from the first defined hotkey label in IncludeTest will also run.
Image


I think this is a bug. Please correct me if I'm wrong.

Best!
Jeremy

IncludeTest.ahk

Code: Select all

;===== AutoExec START =====
global includedScripts
#Include %A_ScriptDir%\IncludeTest\Include1.ahk
#Include %A_ScriptDir%\IncludeTest\Include2.ahk
MsgBox %includedScripts%
;===== AutoExec END =====

;~ This should be skipped on load.
^3:: MsgBox % "Hello from IncludeTest  Ctl+3"
^4:: MsgBox % "Hello from IncludeTest  Ctl+4"
IncludeTest\Include1.ahk

Code: Select all

Goto Include1_EndAutoExec

;~ This should be skipped on load.
MsgBox "Hello from Include1"
;~ ^1:: MsgBox "Hello from Include1"

;~ This should run on load.
Include1_EndAutoExec:
includedScripts := includedScripts . "Loaded: Include1`n"
IncludeTest\Include2.ahk

Code: Select all

Goto Include2_EndAutoExec

;~ This should be skipped on load.
MsgBox "Hello from Include2"
;~ ^2:: MsgBox "Hello from Include2 Ctl+2"

;~ This should run on load.
Include2_EndAutoExec:
includedScripts := includedScripts . "Loaded: Include2`n"
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: #include causing the code from the first hotkey label to run on load.

13 Sep 2016, 03:51

The #Include is irrelevant:
#Include
Causes the script to behave as though the specified file's contents are present at this exact position.
If you replace the #include lines with the full contents of those files, you will get the same result.

The behaviour is by design. The documentation says:
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).
... but it should say:
After the script has been loaded, it begins executing at the top line, continuing until a Return or Exit, the first hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).
... although that's probably too subtle for most readers. When you uncomment ^1::, that becomes the first hotkey. If you skip the first hotkey via goto, then execution will never reach it and it won't cause the auto-execute section to end.

There is a very simple solution to the problem: end your auto-execute section.

Code: Select all

return ;===== AutoExec END =====
Hotkey labels in general do not cause execution to return, and scripts can rely on this:

Code: Select all

^1::
    MsgBox A
^2::
    MsgBox B
    return
; ^1 will show both A and B.
; ^2 will show only B.
The auto-execute section "ends" at the first hotkey (if execution actually reaches it) only because a return is inserted automatically before it. If you replace ^1:: with ctrl_1:, ^2:: will then be the first hotkey, so gosub ctrl_1 will show only A, not B.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: #include causing the code from the first hotkey label to run on load.

13 Sep 2016, 11:28

I was experimenting with this too. I couldn't provide such a thorough explanation as lexikos, but I did make an example that I think adds to the explanation.
Consider the following. It will show MsgBox one and three.

Code: Select all

Goto one
^2::MsgBox, two
one:
MsgBox, one
^3::MsgBox, three
Now comment-out the ^2 hotkey and it will only show MsgBox one.

Code: Select all

Goto one
;~ ^2::MsgBox, two
one:
MsgBox, one
^3::MsgBox, three
Throwing an exception shows the surrounding lines in the MsgBox:

Code: Select all

Goto one
^2::MsgBox, two
one:
throw Exception("ERROR")
MsgBox, one
^3::MsgBox, three

Code: Select all

Error:  ERROR

	Line#
	001: Goto,one
	002: Return
	002: MsgBox,two
	002: Return
--->	004: Throw,Exception("ERROR")
	005: MsgBox,one
	006: MsgBox,three
	006: Return
	007: Exit
	008: Exit
	008: Exit

The thread has exited.
There is an extra return before the first hotkey (on line 2). An extra return is not added prior to the other hotkey (line 6).
guest3456
Posts: 3462
Joined: 09 Oct 2013, 10:31

Re: #include causing the code from the first hotkey label to run on load.

13 Sep 2016, 11:41

^^^ahhhhh

so in actuality, AHK is inputting its own returns to close out the autoexec (as well as single line hotkeys too)

User avatar
GollyJer
Posts: 69
Joined: 19 Sep 2015, 19:33
Contact:

Re: #include causing the code from the first hotkey label to run on load.

13 Sep 2016, 16:45

OK, this all makes sense now.
In reality AHK runs auto-execution until it encounters Return, Exit, or the end of the script.
To facilitate the ability to stop before the first hotkey label AHK silently injects a Return before the first hotkey label.

In my example, Goto skips over the first hotkey label and auto-execution never encounters the injected Return.

AHK also silently injects a Return after single line hotkey labels (but not multiline). This explains why the developer must code their own Return for multi-line, but not single-line, hotkey labels.

In my example, the Goto commands skip over all the "silently injected" Returns until it encounters the Return injected after the line ^3:: MsgBox % "Hello from IncludeTest Ctl+3" which actually looks like this in the eyes of AHK.

Code: Select all

^3:: MsgBox % "Hello from IncludeTest  Ctl+3"
return
Interesting stuff. I know a lot more about AHK auto-execution now. :-)
This clearly is not a bug.

Best!
Jeremy

P.S. - what's the BBCode to highlight commands? Thanks!

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 24 guests