Function only working when called from gui

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bowie1123
Posts: 1
Joined: 05 May 2024, 03:25

Function only working when called from gui

05 May 2024, 03:50

When I call `updateOptions()` from a g-label from a Gui button it works properly, however when it is called from a hotkey definition, MsgBox prints `v` correctly but `val` is blank. Why is this?

Code: Select all

global guiToOptions := {"ItemSearchCheckbox":"ItemSearch"
    ,"CraftCheckbox":"Craft"}

updateOptions() {
    for i, v in guiToOptions {
        GuiControlGet, val,,%i%
        MsgBox % v ", " . val
        options[v] := val
    }
}
User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: Function only working when called from gui

05 May 2024, 05:58

Welcome to this AutoHotkey forum!

The bug is in the part of your script that you have not posted.

Your "options" array has no role in the script that you posted. It would also be local to your function unless declared as global.

If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.
User avatar
Chunjee
Posts: 1447
Joined: 18 Apr 2014, 19:05
Contact:

Re: Function only working when called from gui

05 May 2024, 11:12

If your GUI exists in the global scope I think you can fix this code by the following:

Code: Select all

guiToOptions := {"ItemSearchCheckbox":"ItemSearch"
    ,"CraftCheckbox":"Craft"}

updateOptions() {
    global
    for i, v in guiToOptions {
        GuiControlGet, val,,%i%
        MsgBox % v ", " . val
        options[v] := val
    }
}
I would avoid superglobals personally
just me
Posts: 9514
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Function only working when called from gui

06 May 2024, 03:04

Chunjee wrote:I would avoid superglobals personally
I would avoid assume-global mode personally. ;)

@bowie1123: Does your GUI have a name or a number other than 1?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 138 guests