0

AutoHotkey 命令Menu允许您自定义脚本通知区域图标的上下文菜单,但它似乎需要将标签/子例程附加到菜单条目。

我有一些函数,虽然我可以转换为子例程,但我不希望这样做,因为它们使用起来更容易、更清晰,而且还有需要重构才能转换的本地函数。此外,函数不会自动执行,必须调用,而子例程只是标记的代码段,因此它们需要额外的工作以避免被无意调用。事实上,AHK 手册页Gosub特别建议使用函数:

尽管 Gosub 对于简单的通用子例程很有用,但请考虑将函数用于更复杂的目的。

子程序的一个明显缺点是它们不能接受参数。

有没有办法创建绑定到功能的菜单项?

4

2 回答 2

1

您可以让每个子例程调用相应的函数。

于 2012-09-30T19:16:14.063 回答
1

Subroutines don't automatically execute if you use your return statements properly.

As a rule, have a return at the end of each subroutine or multi-line hotkey (Single-line hotkeys don't need that). (A function's execution terminates at the closing brace, so you don't need a return, unless of course you want to return a value, or end termination at some other point in the function.)

Also, ensure you define any subroutine outside the auto-execute section of the script, to prevent it from automatically executing when the script starts.

More about the "auto-execute" section from AHK docs:

The Top of the Script (the Auto-execute Section) After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.

A script that is not persistent and that lacks hotkeys, hotstrings, OnMessage, and GUI will terminate after the auto-execute section has completed. Otherwise, it will stay running in an idle state, responding to events such as hotkeys, hotstrings, GUI events, custom menu items, and timers.

EDIT: Unfortunately, you still cannot bind direction function calls to menus, because that's not supported. But you could call your functions from within the corresponding subroutines (now that you know how to keep them from running automatically). By exposing some state globally you can eliminate the need to pass arguments to your functions. However, if you don't want to do that, you can simply create variables initialized to whatever values you want to pass to the "bound" function at that point, and then using them to make the equivalent of the the "bound" function call that you wanted to make. For example:

subroutine:
arg1 = <some expression>
arg2 = <some expression>
MyFunction(arg1, arg1)
return

MyFunction(a, b) 
....
}
于 2012-10-05T19:21:41.620 回答