多次使用循环

时间:2017-08-08 05:36:01

标签: loops autohotkey

1。简言之

我不明白,如何在不结束脚本的情况下多次使用来自Loop的周期。

2。设置

我有一个文件SashaAutoScrolling.ahk,内容为:

; First loop, Speed 1
#+1::
Loop
{
    Send {WheelDown}
    Sleep 3000
}

; Second loop, Speed 2
#+2::
Loop
{
    Send {WheelDown}
    Sleep 600
}

; Third loop, Speed 3
#+3::
Loop
{
    Send {WheelDown}
    Sleep 100
}

; Fourth loop, Speed Up
#+0::
Loop
{
    Send {WheelUp}
    Sleep 600
}

; Loop pause
; http://autohotkey.com/board/topic/95308-endless-loop-with-hotkey-pause/?p=600526
#p::Pause

; Exit script
#esc::ExitApp

3。重现的步骤

我在任何PDF查看器中打开任何pdf文件。我在«速度»之间切换:

  • 移+超级+ 3
  • 移+超级+ 0
  • 移+超级+ 2
  • 移+超级+ 3
  • 切换+超级+ 0

4。实际行为

Actual

如果我第一次运行 Shift + Super + 3 Shift + Super + 0

我可以成功切换«速度»。

如果我按 Shift + Super + 3 Shift + Super + 0 第二次,

我无法在«速度»之间切换。

5。预期的行为

成功切换«速度»无限次。

6。没帮忙

  1. 谷歌搜索,在Stack Overflow和AutoHotkey论坛中搜索。
  2. 7。不提供

    1. 请不要使用第三方程序。 Adobe Reader免费版works as expected,但我无法阅读,请使用此程序,例如,djvudoc本书。
    2. 请不要使用built-in mouse auto scrolling。它很不舒服,因为有问题的快速选择准确舒适的“速度”阅读。

3 个答案:

答案 0 :(得分:1)

在按下 Shift + Win + 1 Shift + Win + 2 等模拟鼠标滚轮滚动时滚动当前窗口。 esc 退出循环

#+1:: scrollit(3000)
#+2:: scrollit(600)
#+3:: scrollit(300)
Esc:: abort()

scrollit(delay)
{
  global abort := false
  while (!abort)
  {
    Send {WheelDown}
    Sleep delay
  }
}


abort()
{
  global abort := true
}

答案 1 :(得分:1)

此代码使用模拟鼠标滚轮滚动

滚动当前窗口

Shift + Win + 1 Shift + Win + 2 等...开始滚动。如果已经滚动,只需更新睡眠间隔。 esc 退出

; globals:
;   g_running : true while loop is active
;   g_sleep   : milliseconds to sleep between sending input event
;   g_key     : key to simulate


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; HOTKEY DEFINITIONS

#MaxThreadsPerHotkey 2

#+1:: scrollit(3000)
#+2:: scrollit(600)
#+3:: scrollit(300)
#+0:: scrollit(600,"WheelUp")

#MaxThreadsPerHotkey 1

Esc:: abort()


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; HELPER FUNCTIONS

; send wheeldown every _delay_ microseconds
; if called while loop is already active, just update delay and exit
;
scrollit(sleep, key="WheelDown")
{
  global g_running
  global g_sleep := sleep
  global g_key   := key

  if g_running
    return

  g_running := true

  while (g_running)
  {
    Send {%g_key%}
    Sleep g_sleep
  }
}



abort()
{
  global g_running := false
}

答案 2 :(得分:0)

您的代码无法正常工作,因为默认情况下,每个热键只有一个主题。当你这样做时:

Loop
{
    Send {WheelDown}
    Sleep 100
}

你永远不会从这一个线程返回,所以当你第二次调用它时,热键不会触发,你的程序将继续在当前线程中循环。

Jim U通过调用#MaxThreadsPerHotkey 2解决了这个问题,但这不会让你的代码工作,因为你根本不会从你的热键线程返回,Jim U没有&#39} ; t仅从第一个线程返回,因此2个线程足以让他的solutoin工作。

另一种解决方案是使用计时器,以下将立即改变滚动速度,不需要任何特殊指令,也不依赖于Autohotkeys独特的线程模型。

#+1::callSetTimer(3000)
#+2::callSetTimer(600)
#+3::callSetTimer(100)
#+0::callSetTimer(600, "WheelUp")
Esc::SetTimer, scrollTimer, Off

callSetTimer(interval, key := "WheelDown") {
    global currKey := key
    SetTimer, scrollTimer, %interval%

    scrollTimer:
        send, {%currKey%}
    return
}