How to prevent code execution

Share your ideas as to how the documentation can be improved.
need4speed
Posts: 132
Joined: 22 Apr 2016, 06:50

How to prevent code execution

28 Jul 2020, 05:42

running the code bellow in v1 I get a Warning and a MessageBox:

Code: Select all

#Warn
#SingleInstance Force
MsgBox "Hi", "My Title", 1
^n::  ; CTRL+N hotkey
{
    Run "notepad.exe"  ; Run Notepad when you press CTRL+N.
}  ; This ends the hotkey. The code below this will not get triggered.
MsgBox "Hi 2", "My Title", 1	
esc::exitapp
MsgBox "Hi 3", "My Title", 1
In v2a119 I get no Warning but 3 msgboxes.

I read the help file, and I assumed curly brakets or a hotkey definition would prevent the code execution bellow, but even adding a "return" command did not work.
What am I missing out? Cheers.
swagfag
Posts: 4070
Joined: 11 Jan 2017, 17:59

Re: How to prevent code execution

28 Jul 2020, 07:13

well, where did u put the return?
in v2 hotkeys behave like function definitions, no longer impacting control flow
need4speed
Posts: 132
Joined: 22 Apr 2016, 06:50

Re: How to prevent code execution

28 Jul 2020, 09:58

Thanks swagfag. Apparently I fell for the incorrect v2 help file. In v1 return ends the hotkey, the comment is correct:

Code: Select all

^n::  ; CTRL+N hotkey
Run, notepad.exe  ; Run Notepad when you press CTRL+N.
return  ; This ends the hotkey. The code below this will not get triggered.

Code: Select all

^b::  ; CTRL+B hotkey
Send, {Ctrl down}c{Ctrl up}  ; Copies the selected text. ^c could be used as well, but this method is more secure.
SendInput, [b]{Ctrl down}v{Ctrl up}[/b] ; Wraps the selected text in BBCode tags to make it bold in a forum.
return  ; This ends the hotkey. The code below this point will not get triggered.
the comments or the example code in v2 help doc is incorrect though:

Code: Select all

^n::  ; CTRL+N hotkey
{
    Run "notepad.exe"  ; Run Notepad when you press CTRL+N.
}  ; This ends the hotkey. The code below this will not get triggered.

Code: Select all

^b::  ; CTRL+B hotkey
{
    Send "{Ctrl down}c{Ctrl up}"  ; Copies the selected text. ^c could be used as well, but this method is more secure.
    SendInput "[b]{Ctrl down}v{Ctrl up}[/b]" ; Wraps the selected text in BBCode tags to make it bold in a forum.
}  ; This ends the hotkey. The code below this point will not get triggered.
I'd appreaciate if a native speaker could fix the v2 help file comments and / or exmple code. Cheers.
swagfag
Posts: 4070
Joined: 11 Jan 2017, 17:59

Re: How to prevent code execution

28 Jul 2020, 10:15

yeah, the latter part of the comment is no longer correct as of a111
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How to prevent code execution

28 Jul 2020, 23:06

It won't execute when you press the hotkey, nor will it get "triggered". It will just execute as part of the auto execute thread, if control flow allows.

The return at the end of the hotkey (generally) never affected auto execution in v1, because the auto execute section already ended at the first hotkey label.

I'm not sure I get what you're trying to do. You want to include code that never executes? Why not comment it out, or just delete it?
need4speed
Posts: 132
Joined: 22 Apr 2016, 06:50

Re: How to prevent code execution

29 Jul 2020, 08:32

in v1 one could run this code, :

Code: Select all

; AHK v1
1::
return

MsgBox, In v1 this code will not be executed.
due to the return, the last command would never be executed.

in v2 the syntax has changed:

Code: Select all

; AHK v2
1::
{

}

MsgBox "In v2 this code is executed."
The latter command is executed.

Hence the comment in v2 help ( ; This ends the hotkey. The code below this point will not get triggered.) is not true.
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How to prevent code execution

29 Jul 2020, 22:42

I think you've missed the point of my second paragraph. If you remove the return in v1, MsgBox still won't be called *at startup*. The return only stops it from executing *as part of the hotkey subroutine*, when you press the hotkey. Ending the hotkey function with } prior to MsgBox has the same effect.

In v1, the script's first hotkey label acts as return. This is the hotkey label itself, not the return, and only the first hotkey label. If you want that effect, just add return.

The code is never "triggered" in the first place. I have no idea what the author was thinking.
need4speed
Posts: 132
Joined: 22 Apr 2016, 06:50

Re: How to prevent code execution

30 Jul 2020, 02:30

thanks for the clarification.
User avatar
joedf
Posts: 7793
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: How to prevent code execution

30 Jul 2020, 09:24

[topic moved to correct place as requested by OP]
User avatar
Ragnar
Posts: 336
Joined: 30 Sep 2013, 15:25

Re: How to prevent code execution

10 Aug 2020, 06:00

I've changed it to "The code below this will not be executed when pressing the hotkey." via PR #449. Later this change will also be applied to the v2 docs.

Return to “Suggestions on documentation improvements”

Who is online

Users browsing this forum: No registered users and 1 guest