Vdmir40
Vdmir40

Reputation: 27

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?

Upvotes: 0

Views: 1909

Answers (1)

0x464e
0x464e

Reputation: 6489

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
}

Upvotes: 1

Related Questions