Dynamically Create AutoHotkey Hotkey to Function/Subroutine

后端 未结 3 1591
孤独总比滥情好
孤独总比滥情好 2021-02-04 17:28

The AutoHotkey command Hotkey allows for the creation of dynamic hotkeys at runtime, but its syntax and documentation seems to limit it to built-in or existing labe

3条回答
  •  情话喂你
    2021-02-04 18:03

    Doing exactly what you want isn't possible in AutoHotkey. This is the closest way I can think of.

    Call this file Hotkeys.ahk, and put it in My Documents/AutoHotkey/Lib. Alternatively make a folder called Lib, and put it in the same directory as your main script.

    Hotkeys := {}
    
    Hotkey(hk, fun, p*) {
        global hotkeys
        hotkeys[hk] := {}
        hotkeys[hk].fun := fun
        hotkeys[hk].p := p
        Hotkey, %hk%, HandleHotkey
    }
    
    HandleHotkey:
    hotkeys[A_ThisHotkey].fun(hotkeys[A_ThisHotkey].p*)
    return
    

    Here's an example script that you could use it with.

    Hotkey("e", "msgbox", "foobar")
    
    MsgBox(msg) {
        msgbox % msg
    }
    
    #Include 
    

    The first parameter is the hotkey, the second is the function to call, and everything after that is passed to the function.

提交回复
热议问题