简体   繁体   中英

Autohotkey Toggle script

I simply want to toggle the command with the same keymapping (Ctrl + I):

#InstallKeybdHook
#UseHook

^i::
send, BLABLABLA
return

If I press Ctrl+I, it types BLABLABLA(of course), and I want to make it repeated with a certain interval(180 sec) and I want it to be toggled. How to do it?

You're going to want to use a timer .

And I'm not sure why you're using those two #directives, they're not doing anything useful for that script.

But about using a timer:
SetTimer, TimerCallback, 180000
This creates a timer that fires the function (or label) TimerCallback every 180,000 ms (180 sec).
Of course we're yet to define the function TimerCallback , so lets do that now:

TimerCallback()
{
     Tooltip, hi
}

And then to toggle the timer on/off on a hotkey:

^i::
     toggle := !toggle ;a convenient way to toggle a variable in AHK, see below of explanation
     if (toggle) ;if true
     {
          SetTimer, TimerCallback, 180000 ;turn on timer
          ;the function will only run for the first timer after 
          ;those 180 secs, if you want it to run once immediately
          ;call the function here directly:
          TimerCallback()
     }
     else
          SetTimer, TimerCallback, Off ;turn off timer
return

Explanation for toggle:= !toggle variable state toggling can be found from a previous answer of mine here .
Also includes an example for a sweet little 1liner timer toggling hotkey.


And here's the full example script:

^i::
     toggle := !toggle ;a convenient way to toggle a variable in AHK, see below of explanation
     if (toggle) ;if true
     {
          SetTimer, TimerCallback, 180000 ;turn on timer
          ;the function will only run for the first timer after 
          ;those 180 secs, if you want it to run once immediately
          ;call the function here directly:
          TimerCallback()
     }
     else
          SetTimer, TimerCallback, Off ;turn off timer
return

TimerCallback()
{
     Tooltip, hi
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM