Labels and #if

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1277
Joined: 06 Aug 2017, 08:21
Location: Germany

Labels and #if

01 Apr 2020, 15:44

Hello,

A while ago I reported that AHK refused such a code:

Code: Select all

F1::
F1:
MsgBox, hi
return

F2::
GoTo, F1
return
It says, "Duplicate label".

I was told by person from this forum it was normal because hotkey labels are also labels.
Ok, makes sense, but why does this code not work:

Code: Select all

mode = 1

F1::
mode++
if mode = 3
{
  mode = 1
}
return

F3::
GoTo, mylabel
return

#if mode = 1
mylabel:
F2::
MsgBox, first mode
return
#if

#if mode = 2
mylabel:
F2::
MsgBox, second mode
return
#if
It accepts the two F2 labels, but refuses the two mylabel labels.
How can I get it work?

Is the only chance something like:

Code: Select all

mode = 1

F1::
mode++
if mode = 2
{
  mode = 1
}
return

F3::
GoTo, mylabel
return

#if mode = 1
F2::
MsgBox, first mode
return
#if

#if mode = 2
F2::
MsgBox, second mode
return
#if

mylabel:
if mode = 1
{
  MsgBox, first mode
}
if mode = 2
{
  MsgBox, second mode
}
return
Well, I can live with that, but it's inconsistent and I really would prefer to split them with #if like done with the hotkey labels.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Scr1pter
Posts: 1277
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Labels and #if

01 Apr 2020, 15:52

Just had this idea, and it worked:

Code: Select all

mode = 1

F1::
mode++
if mode = 3
{
  mode = 1
}
return

F3::
GoTo, mylabel%mode%
return

#if mode = 1
mylabel1:
F2::
MsgBox, first mode
return
#if

#if mode = 2
mylabel2:
F2::
MsgBox, second mode
return
#if
I believe that's the shortest way.
It doesn't matter where the mylabels are, but for a better overview (for me), I put them inside of #if
Anyway, if somebody has a better idea, let me know.

Using #if is in my opinion still the best way.
(Like it works for hotkey labels.)

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
boiler
Posts: 17072
Joined: 21 Dec 2014, 02:44

Re: Labels and #if

01 Apr 2020, 15:54

It's not two labels it they're hotkeys preceded by different #If directives. It's two different variants of the same hotkey label which is allowed. Details here.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Archimede, RSable, Spikea and 326 guests