2
votes

Is it possible to create state dependent hotkeys with AutoHotKey? I know it is possible to create certain hotkeys in a dependent state when working with #IfWinActive but how about creating hotkeys inside a hotkey itself?

Example

F1::
    Gui, Show, center center h500 w500, Just a window
    return

    F2::
        MsgBox, 0, F2, You have pressed F2 inside the F1 hotkey
    return
return

The problem here is that F2 is not state dependent and will trigger as soon as you press it.

Possible solution

F1::
    state := true

    Gui, Show, center center h500 w500, Just a window
    return

    #If (state = true)
    {
        F2::
            MsgBox, 0, F2, You have pressed F2 inside the F1 hotkey
        return
    }

    GUIclose:
        Gui, destroy
        state := false
    return
return

This is a possible solution and works very well. However, is there an easier solution to it than doing it this way?

2
As far as I'm aware of, there is no better way. I'd say you pretty much nailed it and the code looks rather clean. Good job.errorseven

2 Answers

1
votes

Not quite "easier", but imo better looking and my preferred way:

F1::
    Hotkey, F2, F2_action, ON
    Gui, Show, center center h500 w500, Just a window
return

F2_action: ; this is a label not a hotkey
    MsgBox, 0, F2, You have pressed F2 inside the F1 hotkey
return

GUIclose:
    Gui, destroy
    Hotkey, F2, F2_action, OFF
return

I made use of the hotkey-command here, which is specifically made for

but how about creating hotkeys inside a hotkey itself

1
votes

I would go the route of adding 2 conditions to an #If

F1::
    state := true
    Gui, Show, center center h500 w500, Just a window
Return

#If state and WinActive("Just a window")
    F2::MsgBox, 0, F2, You have pressed F2 inside the F1 hotkey
#If

Not sure if this is what you already ruled out when stating you tried #IfWinActive