动态创建函数/子程序的 AutoHotkey 热键 [英] Dynamically Create AutoHotkey Hotkey to Function/Subroutine
问题描述
AutoHotkey 命令Hotkey
允许在运行时创建动态热键,但其语法和文档似乎将其限制为内置或现有标签/子例程,这使得它很多 不太有用:
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 labels/subroutines, which makes it much less useful:
热键、键名 [、标签、选项]
Hotkey, KeyName [, Label, Options]
有没有办法让它像普通的硬编码热键一样工作?例如:
Is there a way to get it to work like regular, hard-coded hotkeys? For example:
#z::MsgBox foobar ; Typical, hard-coded hotkey pops up a message-box
Hotkey, z, MsgBox foobar ; Nope; complains about missing label "MsgBox foobar"
由于手册中的以下行,看起来它可能是可能的,但是尚不清楚它是如何工作的:
It looks like it might be possible due to the following line from the manual, however it is not clear how it would work:
标签 - 可以使用普通标签和热键/热字符串标签.
Label - Both normal labels and hotkey/hotstring labels can be used.
推荐答案
在 AutoHotkey 中无法完全按照您的意愿行事.这是我能想到的最接近的方式.
Doing exactly what you want isn't possible in AutoHotkey. This is the closest way I can think of.
调用这个文件Hotkeys.ahk
,并将其放入My Documents/AutoHotkey/Lib
.或者,创建一个名为 Lib 的文件夹,并将其放在与主脚本相同的目录中.
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.
这篇关于动态创建函数/子程序的 AutoHotkey 热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!