You can script a 'lookup table' which holds hotkey names along with their associated function. Then, assign all hotkeys to one label, which gets the function name from the lookup table (using A_ThisHotkey) and calls the function dynamically.
In AHK_L it would be more efficient to store a function reference using Func().
A similar setup could work for gui g-labels, though any workaround would be much less elegant than simply specifying a function in the gui control's options (or instead of the hotkey label).
How is elegance defined as it relates to coding? There are some programmers in these forums who use a single character or an arcane symbol for variable names and call it elegant. :x Sorry, had to get that off my chest. It just drives me nuts. :lol:
Unless somehow adding the ability to pass parameters along with the function call, it would have no benefit over the workarounds mentioned above.
The advantage of a function lookup table is that passing parameters is easy and efficient.
A short AHK_L example:
#NoEnv
#SingleInstance force
SetBatchLines -1
ListLines Off
SendMode Input
SetWorkingDir %A_ScriptDir%
; Autoexecute
gosub MakeGui
Table := []
Table["Color", 1] := Func("ListColors") ; Column 1 contains function references
Table["Color", 2] := ["red", "green", "blue"] ; Column 2 contains function parameters
Gui Show
return
MakeGui:
Gui add, Button
, w75 gLookUp ; All commands gosub the LookUp subroutine
, Color
return
LookUp:
; Use an indirect, varadic function call
ReturnValue := Table[A_GuiControl, 1].(Table[A_GuiControl, 2]*)
return
GuiClose:
ExitApp
return
ListColors(Params*) {
for k, v in Params
MsgBox % v
}