AutoHotkey Community

It is currently May 27th, 2012, 12:56 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: September 20th, 2011, 5:49 am 
Offline

Joined: March 16th, 2011, 6:12 pm
Posts: 172
Location: Worcester, Massachusetts
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

Code:
Goto Label
Label:
^a::MsgBox, Displays


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.

Code:
;any uncommented code between the label and the hotkey will fix bug
Goto Label
Label:
v =
^a::MsgBox, OK


Code:
;the goto does not have to occur on load
^b::Goto Label
Label:
^a::MsgBox, NO


Code:
;multiline hotkeys
Goto Label
Label:
^a::
MsgBox, OK
Return


Code:
;same behavior with gosub
Gosub Label
Label:
^a::MsgBox, NO


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

Code:
#Include File.ahk
^a::MsgBox, OK

;Contents of included file:
< empty >


Code:
#Include File.ahk
^a::MsgBox, NO

;Contents of included file:
Goto Label
Label:


Code:
#Include File.ahk
^a::MsgBox, OK

;Contents of included file:
Goto Label
Label:
v =


Code:
#Include File.ahk
v =
^a::MsgBox, OK

;Contents of included file:
Goto Label
Label:


Code:
;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 =
^a::MsgBox, NO

;Contents of included file:
Goto Label
^b::MsgBox, Another hotkey
Label:
v =


Notes
  • The script does not continue executing after the problematic line
  • The hotkey works as normal
  • Tested on vanilla and latest version of L
  • %A_ThisHotkey% is blank

_________________
★★★ Email me at berban at aim full stop com ★★★


Last edited by berban_ on September 23rd, 2011, 6:38 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 20th, 2011, 6:15 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
What makes you think it's a bug?
Quote:
Bug Reports
Report problems with documented functionality.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 20th, 2011, 6:55 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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).
berban_ wrote:
Code:
;does not trigger multiline hotkeys
Goto Label
Label:
^a::
MsgBox, OK
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 20th, 2011, 7:37 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
It's not just "technically not a bug"; it's intended behaviour. I will attempt to explain.
doc wrote:
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:
Code:
^a::MsgBox
Code:
^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...
Code:
label1:
label2:
^a::
    MsgBox
... all three labels point at MsgBox.
doc wrote:
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:
Code:
<auto-execute section>
label:
<any command or expression>  ; 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:
Code:
<auto-execute section>
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:
Code:
goto label
^a::  ; This ends the auto-execute section.
label:
    x=y
^b::
    MsgBox % x
    ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 20th, 2011, 8:08 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Lexikos wrote:
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.
Lexikos wrote:
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.
Code:
^a::
^a:  ; Error: duplicate label.
Code:
^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.


Last edited by jaco0646 on November 30th, 2011, 4:23 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2011, 6:56 am 
Offline

Joined: March 16th, 2011, 6:12 pm
Posts: 172
Location: Worcester, Massachusetts
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.

jaco0646 wrote:
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.

_________________
★★★ Email me at berban at aim full stop com ★★★


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group