autohotkey

with the 'hotkey' command, how can I enable/disable a lot of hotkeys at once?


I have been trying to learn more about the hotkey command, I am stuck on a aspect of it. Lets say my script starts with a series of hotkeys enabled, initialised with the double colon, but at some point later I would like to momentarily disable all these hotkeys in one go.

For example, lets say a, b, c etc etc are hotkeys the script starts with, when I press f1 I would like to disable all of them. So I came up with the following which works:

a::
    tooltip, a key is pressed
    return
b::
    tooltip, b key is pressed
    return
c::
    tooltip, c key is pressed
    return
; imagine many more hotkeys here ...

f1::
    Hotkey, a, toggle
    Hotkey, b, toggle
    Hotkey, c, toggle
    ;list more hotkeys to disable
    ;line
    ;by
    ;line??
    tooltip hot keys have been toggled
    return

Its not practical, as I am required to list every hotkey I need to disable line by line. And I intend to use the code under f1 in a lot of places, so I need it to be compact. I am wondering is there a way to use the hotkey command to disable a number of hotkeys in one go.

I can disable a number of hotkeys with the following, but my object is to learn the hotkey command here and I am wondering if there is a way to do the following with it:

defaultHotkeys := 1
#if (defaultHotkeys)
    a::
        tooltip, a key is pressed
        return
    b::
        tooltip, b key is pressed
        return
    c::
        tooltip, c key is pressed
        return
    ; imagine many more hotkeys here ...
#if

f1::
    defaultHotkeys := defaultHotkeys ? 0 : 1
    tooltip hot keys have been toggled
    return

Is this what Hotkey, If , Expression is designed for? The docs mentions it but I dont fully understand it. Thank you for any help.


Solution

  • You can create arrays of those hotkeys in the auto-execute section. and use a For-Loop to disable/re-enable each of them using the hotkey command:

    #UseHook
    
    toggled1 = 0
    toggled2 = 0
    
    Keys_1 := ["a","c","e"]
    Keys_2 := ["b","d"]
    
    a::
    b::
    c::
    d::
    e:: tooltip %A_ThisHotkey% is pressed,,, 1
    
    f1::
        toggled1 := !toggled1
        For each, key1 in Keys_1
            Hotkey, %key1%, toggle
        If (toggled1)
            tooltip Keys_1 have been disambled,,, 2
        else
            tooltip Keys_1 have been re-enabled,,, 2
    return
    
    f2::
        toggled2 := !toggled2
        For each, key2 in Keys_2
            Hotkey, %key2%, toggle
        If (toggled2)
            tooltip Keys_2 have been disambled,,, 3
        else
            tooltip Keys_2 have been re-enabled,,, 3
    return
    

    To toggle all hotkeys in the script use the Suspend command.