代码之家  ›  专栏  ›  技术社区  ›  Aleksandr Makov

如何并行启动2个循环?

  •  0
  • Aleksandr Makov  · 技术社区  · 8 年前

    我正在努力做到以下几点:

    ~e::
    while GetKeyState("e","P")
    {
        Send {1 Down}
        Sleep 500
        Send {1 Up}
    }
    
    while GetKeyState("e","P")
    {
        Send {2 Down}
        Sleep 1000
        Send {2 Up}
    }
    

    回来

    注意睡眠差异。仅执行键1循环。

    我两个都试过了 gosub -s和 goto -s、 但他们只是从结果开始。当按下一个键时,如何启动并保持两个循环同时运行?

    非常感谢。

    1 回复  |  直到 8 年前
        1
  •  1
  •   stevecody    8 年前

    自动热键不能[同时]和[在同一脚本中]执行循环

    您可以使用两个单独的Ahk脚本,同时可以执行循环(parralel),

    您可以尝试以下代码:

    ; [+ = Shift] [! = Alt] [^ = Ctrl] [# = Win] 
    #notrayicon
    #SingleInstance force
    
    
    ConvertAndRun1("~e:: | while GetKeyState('e','P') | { | Send {1 Down} | Sleep 500 | Send {1 Up} | } | return | ~esc::exitapp")
    ConvertAndRun2("~e:: | while GetKeyState('e','P') | { | Send {2 Down} | Sleep 1000 | Send {2 Up} | } | return | ~esc::exitapp")
    exitapp
    
    ConvertAndRun1(y)
    {
    FileDelete, Runscript1.ahk
    ;a - [Convert String into single codelines with return] - [Char | is the breakline.]
    StringReplace,y,y, |, `n, all
    StringReplace,y,y, ', ", all
    sleep 150
    
    ;b - Save String to Ahk file 
    x:=";#notrayicon `n #SingleInstance force `n "
    z:="`n "
    FileAppend, %x%%y%%z%, Runscript1.ahk
    sleep 150
    
    ;c - Now it can Run all these commands from that Ahk file.
    run Runscript1.ahk
    sleep 150
    }
    
    
    ConvertAndRun2(y)
    {
    FileDelete, Runscript2.ahk
    ;a - [Convert String into single codelines with return] - [Char | is the breakline.]
    StringReplace,y,y, |, `n, all
    StringReplace,y,y, ', ", all
    sleep 150
    
    ;b - Save String to Ahk file 
    x:=";#notrayicon `n #SingleInstance force `n "
    z:="`n "
    FileAppend, %x%%y%%z%, Runscript2.ahk
    sleep 150
    
    ;c - Now it can Run all the commands from that ahk file.
    run Runscript2.ahk
    sleep 150
    exitapp
    }
    

    注:

    1-我制作了一个脚本,可以使用多个命令将单行(换行符=|)转换为单个Ahk文件。

    2-在单行中,您需要使用[']而不是双引号[”]

    3-使用[Esc]键可以停止两个脚本。