Help Making Toggle Loop Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
toxicxarrow
Posts: 2
Joined: 01 Jun 2018, 15:34

Help Making Toggle Loop

01 Jun 2018, 15:42

Hello, I'm trying to make a script in AutoHotkey that toggles "scroll lock" on and off every 750 milliseconds with the press of a key combination (ctrl+alt+i). If I press it again, it stops the loop. This is what I have so far:

Code: Select all

Ctrl-Alt-i::
Toggle := !Toggle
loop
{
    If not Toggle
        break
    scrollLock
}
return
Any help would be appreciated.
User avatar
Xtra
Posts: 2038
Joined: 02 Oct 2015, 12:15

Re: Help Making Toggle Loop  Topic is solved

01 Jun 2018, 19:18

Code: Select all

#NoEnv

^!i::SetTimer, MyLoop, % (Toggle := !Toggle) ? 750 : "Off"

MyLoop:
    Send, {ScrollLock}
return
HTH
toxicxarrow
Posts: 2
Joined: 01 Jun 2018, 15:34

Re: Help Making Toggle Loop

02 Jun 2018, 14:10

Thank you so much. That's exactly what I needed.
DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: Help Making Toggle Loop

03 Jun 2018, 07:36

Xtra wrote:

Code: Select all

#NoEnv

^!i::SetTimer, MyLoop, % (Toggle := !Toggle) ? 750 : "Off"

MyLoop:
    Send, {ScrollLock}
return
HTH
Hey this is a nice way to boost versatility of settimer. Its not clear in docs that u can expand the period parameter like this. Could you please comment on it? :)
gregster
Posts: 5550
Joined: 30 Sep 2013, 06:48

Re: Help Making Toggle Loop

03 Jun 2018, 07:48

DRocks wrote:Hey this is a nice way to boost versatility of settimer. Its not clear in docs that u can expand the period parameter like this. Could you please comment on it? :)
This is nothing limited to SetTimer - you can use the ternary operator in a lot of cases to replace a simple if-else-comparison:
docs wrote:?: Ternary operator [v1.0.46+]. This operator is a shorthand replacement for the if-else statement. It evaluates the condition on its left side to determine which of its two branches should become its final result. For example, var := x>y ? 2 : 3 stores 2 in Var if x is greater than y; otherwise it stores 3. To enhance performance, only the winning branch is evaluated (see short-circuit evaluation).
(https://autohotkey.com/docs/Variables.htm#operators)

But here it was combined with the possibility of forcing an expression: https://autohotkey.com/docs/Variables.htm#Expressions (at the end of the section)
DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: Help Making Toggle Loop

03 Jun 2018, 20:08

gregster wrote:
DRocks wrote:Hey this is a nice way to boost versatility of settimer. Its not clear in docs that u can expand the period parameter like this. Could you please comment on it? :)
This is nothing limited to SetTimer - you can use the ternary operator in a lot of cases to replace a simple if-else-comparison:
docs wrote:?: Ternary operator [v1.0.46+]. This operator is a shorthand replacement for the if-else statement. It evaluates the condition on its left side to determine which of its two branches should become its final result. For example, var := x>y ? 2 : 3 stores 2 in Var if x is greater than y; otherwise it stores 3. To enhance performance, only the winning branch is evaluated (see short-circuit evaluation).
(https://autohotkey.com/docs/Variables.htm#operators)

But here it was combined with the possibility of forcing an expression: https://autohotkey.com/docs/Variables.htm#Expressions (at the end of the section)
Very interesting and thanks alot for taking time :)
Have a great day

Return to “Ask For Help”

Who is online

Users browsing this forum: Melchisedek, NicolaLearnsAHK, songdg, tatermakes and 56 guests