【问题标题】:Autohotkey Single line code?Autohotkey 单行代码?
【发布时间】:2018-06-24 17:47:41
【问题描述】:

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

例如这是一个工作代码

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

这不是

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

【问题讨论】:

    标签: expression autohotkey


    【解决方案1】:

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

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

    你可以试试这个,

    1 - 将其放入字符串中。

    2 - 转换 + 保存到文件。

    3 - 然后从外部文件运行脚本。

    注意 - 我确实制作了脚本,所以你确实想要它,保存它 + ?Reload=run 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,则可以从那里保存并运行该文件,它会增加速度 - Ramdisk 是一个虚拟磁盘(存储在 RAM MEMORY z:) 上,比硬盘快 +-100 倍。 - 你可以从这里找到这个免费工具 (Imdisk)

    【讨论】:

      【解决方案2】:

      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
      }
      

      【讨论】:

        【解决方案3】:

        据我所知,我认为这是不可能的,因为换行符起到了语句分隔符的作用,终止了前面的命令/表达式。

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

        换行是有意义的:换行通常充当语句分隔符,终止前一个命令或表达式。 (语句只是语言中最小的独立元素 表示要执行的某些操作。) 是续行

        【讨论】:

        • 我明白你的意思,但可能有一些方法
        • 你能解释为什么你需要把它放在一条线上吗?你看过这个链接吗? autohotkey.com/board/topic/60052-multiple-commands-on-a-line
        • 谢谢很多,但它仍然不是我要找的。我想在一行中完成所有事情(pix.search、loops、if vs.),比如 funk 和 send。
        【解决方案4】:

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

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

        banana:
        Send, ^s
        Send, hello world
        return
        

        ...以下行就足够了:

        right::Gosub, banana
        

        【讨论】:

          猜你喜欢
          • 2015-01-25
          • 1970-01-01
          • 2018-07-31
          • 1970-01-01
          • 1970-01-01
          • 2019-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多