问题描述
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:
有没有办法让它像常规的硬编码热键一样工作?例如:
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:
推荐答案
在AutoHotkey中完全不了解您想要的内容。这是我可以想到的最接近的方式。
Doing exactly what you want isn't possible in AutoHotkey. This is the closest way I can think of.
调用此文件 Hotkeys.ahk
,并将其放在我的文档/ 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热键功能/子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!