Issue with Label names and hotkeys

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Scr1pter
Posts: 1185
Joined: 06 Aug 2017, 08:21
Location: Germany

Issue with Label names and hotkeys

14 Mar 2020, 18:49

Hello,

I've noticed that this script will not run because AHK thinks the F1 label exists twice.

Code: Select all

F1::
MsgBox, Hotkey F1 was pressed.
return

F1:
MsgBox, Label F1 was executed.
return

F2::
GoSub F1
return
However, one is a label, the other is a hotkey.
I checked this page and it only suggests not to use "On, Off, Toggle, AltTab, ShiftAltTab, AltTabAndMenu and AltTabMenuDismiss"
https://www.autohotkey.com/docs/misc/Labels.htm#hotkeys-and-hotstrings

I like to use Labels like A1 until H8 to have tiles to execute functions.
(Like a chessboard).

Any chance to use F1 hotkey AND F1 label?

P.S: AHK version 1.1.32.0

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602, Jelly Comb Touchpad - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.32.00
gregster
Posts: 4461
Joined: 30 Sep 2013, 06:48

Re: Issue with Label names and hotkeys

14 Mar 2020, 18:54

Hotkey labels are still labels.
https://www.autohotkey.com/docs/misc/Labels.htm#hotkeys-and-hotstrings wrote:Hotkey and hotstring labels are also valid targets for Goto, Gosub and other commands.
I don't see how this is a bug, this is clearly documented. I'll move it to 'Ask For Help'.

I mean, you could identify by which hotkey the F1:: label was called and then do different things:

Code: Select all

F1::
msgbox % A_ThisHotkey
return

F2::
GoSub F1
return
joefiesta
Posts: 377
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Issue with Label names and hotkeys

15 Mar 2020, 11:01

I see that the LABELS help section defines Hotkeys as labels, as Gregster pointed out. However, the general section on Hotkeys does not SPECIFICALLY state a hotkey is a label. It does,however, imply it, by mentioning that the string is a label.

This brings to light a documentation issue (as does almost everything with AHK!).

If the Hotkey is a LABEL, then the definition for the syntax of a LABEL at the LABEL help is not entirely correct. Label says
A label identifies a line of code, and can be used as a Goto target or to form a subroutine.

LabelName:

To create a label, write the label name followed by a colon as shown above. Aside from whitespace and comments, no other code can be written on the same line.


The last proviso is incorrect, inasmuchas it does not apply to a one-line hotkey defintion, for example MyHotkeyLabel:: msgbox Hi Joe
joefiesta
Posts: 377
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Issue with Label names and hotkeys

15 Mar 2020, 11:04

@Scripter : Here is a solution, rather simple for your problem. Add an "*" to the Hotkey label.
AHK considers the label for a hotkey everything other than the trailing "::".

*F1::
MsgBox, Hotkey F1 was pressed.
return

F1:
MsgBox, Label F1 was executed.
return

F2::
GoSub F1
return
Helgef
Posts: 4408
Joined: 17 Jul 2016, 01:02
Contact:

Re: Issue with Label names and hotkeys

15 Mar 2020, 12:16

Adding * enables the hotkey to be triggered with any modifier, which might be undesirable. If you really want to work around like that, using the $ prefix is probably better.

Cheers.

Return to “Ask For Help”

Who is online

Users browsing this forum: Ben, Forgetabyteit, Google [Bot], kdservices, tee_rock, vsub and 137 guests