Dynamically Create AutoHotkey Hotkey to Function/Subroutine

若如初见. 提交于 2019-12-03 03:18:48

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 <Hotkeys>

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

This is a refinement of FakeRainBrigand's answer. It is used exactly the same:

Hotkey("x", "Foo", "Bar") ; this defines:  x:: Foo("Bar")

Changes from the original:

  1. Prevent accidental auto-execute of the handler subroutine by tucking it into the function.

  2. Allowing me to reduce namespace pollution by narrowing the scope of the hotkeys variable from global to static.

  3. Optimizations: fun is looked up only once (using Func()) at hotkey definition time; At invocation time, object lookups reduced four to two by splitting hotkeys into two objects funs and args;

This still relies of course on the _L version of AutoHotKey because of Object notation and variadic arg* syntax.

Hotkey(hk, fun, arg*) {
    Static funs := {}, args := {}
    funs[hk] := Func(fun), args[hk] := arg
    Hotkey, %hk%, Hotkey_Handle
    Return
Hotkey_Handle:
    funs[A_ThisHotkey].(args[A_ThisHotkey]*)
    Return
}

Is this what you are looking for?

#Persistent
#SingleInstance Force
#installKeybdHook

Hotkey, #z, MyLabel

MyLabel:
    MsgBox,OK
Return
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!