Search code examples
autohotkey

Calling a custom made functions from the Hotkey command


for example i made this custom function , the grid command which has hotkey of ( ctrl + ' )

grid_command:
send {ctrl down}{' down}
send {ctrl up}{' up}
ToolTipFont("s10", "Segoe UI")
ToolTip Toggle Grid
SetTimer, RemoveToolTip, 300
return

my usual approach is this

!g::
If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 400)
send {ctrl down}{' down}   ;<== Note : this is from grid command
send {ctrl up}{' up}
else
Menu, PS_Grouping, Show
return

but i want a cleaner way ( this is my goal ) see below code

!g::
If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 400)
send {grid_command} ; < instead of using {ctrl down}{' down} i use grid_command
else
Menu, PS_Grouping, Show
return

question 1 : is it possible to implement this in AHK ? if so how ? the code above is not working unfortunately , im pretty sure send was wrong

question 2 : can i do also multiple call from function not only one ?

ex. 

!g::
If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 400)
send {grid_command}
sleep 30
send {another_command}
else
Menu, PS_Grouping, Show
return

thank you for answering

i tried using send and im pretty sure im using the wrong syntax


Solution

    To call a labeled subroutine from another place in the script, use the Gosub command:

    !g::
        If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 400)
            Gosub, grid_command
        else
            Menu, PS_Grouping, Show
    return
    

    If an If- or Else-statement owns more than one line, those lines must be enclosed in braces (to create a block):

    !g::
        If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 400)
        {
            Gosub, grid_command
            sleep 30
            do something else
        }
        else
            Menu, PS_Grouping, Show
    return