Autohotkey 单行代码?

Autohotkey Single line code?

我有一个简单的问题,我无法通过搜索互联网找到答案。
我怎样才能把我的台词变成一行(单行)?

例如这是一个工作代码

right:: 
Send, ^s
Reload 
Send, hello world

这不是

right::Send, ^s::Reload::Send, hello world

据我所知,我认为这是不可能的,因为换行符起到语句分隔符的作用,终止前面的command/expression。

来自文档:https://autohotkey.com/docs/Language.htm

Line breaks are meaningful: Line breaks generally act as a statement separator, terminating the previous command or expression. (A statement is simply the smallest standalone element of the language that expresses some action to be carried out.) The exception to this is line continuation

Autohotkey 没有像 c 中的 ; 和 javascript.

这样的语句终止符

但是,如果您的目标是编写紧凑且可读的代码,使用函数进行重构可以帮助您实现这一目标。

您的代码用一行表示:

right::alpha("^s", "hello world")

alpha(text1, text2) {
  send % text1
  tooltip "reload" will interrupt your script. Any code following "reload" may not execute
  send % text2
}

在 Autohotkey 中,您不能将换行符用于单个代码行。

但是如果你想把行变成一行(单行)。

你可以试试这个,

1 - 将其放入字符串中。

2 - 转换它 + 将它保存到一个文件。

3 - 然后 运行 来自外部文件的脚本。

注意 - 我确实制作了脚本所以你想要它,保存它 + ?Reload=运行 example1.ahk + 发送文本(它不能准确工作所以你喜欢它, 但差不多了。我不清楚你为什么要重新加载脚本?)

试试这个代码。

example1.ahk

; [+ = Shift] [! = Alt] [^ = Ctrl] [# = Win] 
#SingleInstance force
sleep 100
return

right:: ConvertAndRun("Sendinput, ^s | run example1.ahk | runwait example1.ahk | Send, hello world")
left:: ConvertAndRun("Sendinput, ^s | run example1.ahk | runwait example1.ahk | Send, it works")
~esc::exitapp

ConvertAndRun(y)
{
FileDelete, Runscript.ahk
;1 - Convert the String in single codeline's with return - Character | is the breakline.
StringReplace,y,y, |, `n, all
sleep 150

;2 - Save the String to a file 
x:="#notrayicon `n "
z:="`n "
FileAppend, %x%%y%%z%, Runscript.ahk
sleep 150

;3 - Now it can Run all the commands from that string.
run Runscript.ahk
sleep 150
exitapp
}
;

注意 - 此脚本确实将字符串保存到一个文件(在硬盘 c: 上),速度不是那么快,但是如果您使用 Ramdisk, then you can save and run the file's from there, it will increase the speed - a Ramdisk is a virtual Disk (stored on the RAM MEMORY z:) and that is +-100x faster then a Hard disk. - You can find this Free Tool from Here (Imdisk)

作为初学者,我可能会使用子例程而不是函数(就像 Jim U 的回答),不是因为它更好,而是因为我发现它更容易理解。还不习惯ahk中的功能。

如果您将以下代码放在脚本底部(或任何不会打扰您或脚本的地方)...

banana:
Send, ^s
Send, hello world
return

...以下行就足够了:

right::Gosub, banana