Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Question about labels [RESOLVED]


  • Please log in to reply
5 replies to this topic
berban_
  • Members
  • 202 posts
  • Last active: Aug 05 2014 11:52 PM
  • Joined: 16 Mar 2011
Self explanatory. Something about Goto can cause hotkey lines to execute inappropriately.

When you run the below script, the MsgBox will display as if you pressed ^a

Goto Label
Label:
[color=red]^a::MsgBox, Displays[/color]

Here are some more examples to give a fuller picture. Orange text symbolizes changes. The MsgBox only display if it is red. If it is olive it worked correctly and did not display.

;any uncommented code between the label and the hotkey will fix bug
Goto Label
Label:
[color=orange]v =[/color]
[color=olive]^a::MsgBox, OK[/color]

;the goto does not have to occur on load
[color=orange]^b::[/color]Goto Label
Label:
[color=red]^a::MsgBox, NO[/color]

;multiline hotkeys
Goto Label
Label:
[color=orange]^a::[/color]
[color=red]MsgBox, OK[/color]
[color=orange]Return[/color]

;same behavior with gosub
Gosub Label
Label:
[color=red]^a::MsgBox, NO[/color]

The following use an #Included file, the contents of which are also displayed.

[color=orange]#Include File.ahk[/color]
[color=olive]^a::MsgBox, OK[/color]

;Contents of included file:
[color=orange] < empty >[/color]

#Include File.ahk
[color=red]^a::MsgBox, NO[/color]

;Contents of included file:
[color=orange]Goto Label
Label:[/color]

#Include File.ahk
[color=olive]^a::MsgBox, OK[/color]

;Contents of included file:
Goto Label
Label:
[color=orange]v =[/color]

#Include File.ahk
[color=orange]v =[/color]
[color=olive]^a::MsgBox, OK[/color]

;Contents of included file:
Goto Label
Label:

;does not work at all if there is a hotkey in the #Include file
;However, using the hotkey COMMAND is alright.
#Include File.ahk
v =
[color=red]^a::MsgBox, NO[/color]

;Contents of included file:
Goto Label
[color=orange]^b::MsgBox, Another hotkey[/color]
Label:
v =

Notes
[*:2003hid0]The script does not continue executing after the problematic line
[*:2003hid0]The hotkey works as normal
[*:2003hid0]Tested on vanilla and latest version of L
[*:2003hid0]%A_ThisHotkey% is blank

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
What makes you think it's a bug?

Bug Reports
Report problems with documented functionality.



jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
I agree: this is not technically a bug; but to be fair, the fact that the syntax and behavior of labels is not documented means that every AHK script which has ever used a subroutine is relying on undocumented behavior. That's a scary thought. Labels at least deserve their own page in the documentation, separate from GoSub since there are obviously many ways to execute a label.

By the way, this label does trigger its hotkey for me (AHK_L v1.1).

;does not trigger multiline hotkeys
Goto Label
Label:
[color=orange]^a::[/color]
[color=olive]MsgBox, OK[/color]
[color=orange]Return[/color]



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
It's not just "technically not a bug"; it's intended behaviour. I will attempt to explain.

However, if a hotkey needs to execute only a single line, that line can be listed to the right of the double-colon. In other words, the return is implicit

In other words, these are exactly equivalent:
^a::MsgBox
^a::
    MsgBox
    return
There is no distinction between "single-line" and "multi-line" hotkeys.

A label points at the next line of executable code; that is, a command, assignment or expression. ^a:: and ::hotstring:: are also labels, not executable code. So when you write...
label1:
label2:
^a::
    MsgBox
... all three labels point at MsgBox.

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).

If there is a line between the label and hotkey, that line is what the label points at. In that case, the label is inside the auto-execute section, which ends the same way as if the label wasn't there:
[color=#666666]<auto-execute section>[/color]
label:
[color=#666666]<any command or expression>[/color]  ; label points at this line.
^a::  ; This ends the auto-execute section.
    MsgBox
On the other hand, if there is no executable line between the label and the hotkey, the label points at the first line of the hotkey:
[color=#666666]<auto-execute section>[/color]
label:
^a::  ; This ends the auto-execute section,
    MsgBox  ; but label still points at this line.
Furthermore, since the auto-execute section has already ended, subsequent hotkeys do not have this behaviour. In the following example, execution will always fall through the ^b hotkey label:
goto label
^a::  ; This ends the auto-execute section.
label:
    x=y
^b::
    MsgBox % x
    ExitApp


jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006

A label points at the next line of executable code; that is, a command, assignment or expression. ^a:: and ::hotstring:: are also labels, not executable code.

This is exactly the kind of information which should be documented on a page dedicated to labels.

EDIT: More excellent material for the page.

In fact, hotstrings and hotkeys borrow their behaviour from ordinary labels, which is why you can use gosub/goto with them. The only difference of note is that ordinary labels don't "terminate" the auto-execute section, and must be unique.

^a::
^a:  ; Error: duplicate label.
^a:
^a::  ; No error.


EDIT2: The syntax for defining labels is undocumented as well. Show me any subroutine, and I can ask, "How do you know that label name is valid?" Likewise, how does one know whether single-line subroutines are allowed?

EDIT3: The documentation has been updated in AutoHotkey_L v1.1.05.03.

berban_
  • Members
  • 202 posts
  • Last active: Aug 05 2014 11:52 PM
  • Joined: 16 Mar 2011
Thank you both for your thorough & informative replies! I much better understand how labels work now. Before I had thought of them as simply moving execution from one place to another. But now I see it's different.

I agree: this is not technically a bug; but to be fair, the fact that the syntax and behavior of labels is not documented...


Yup, I am a total newb when it comes to programming. All that I know about AutoHotkey comes from the documentation. Fortunately, the documentation is excellent and can bring a total newb to almost complete understanding about ahk and basics of coding. But of course it isn't exhaustive, and eventually you have to complement it with other sources.
Although I would second the idea to include a page on labels, and perhaps a few other concepts like window IDs and classes.