简体   繁体   中英

Auto Hotkey Asynchronous Loop

This is my code:

toggle:=0

z::
    if GetKeyState("LButton") {
    toggle:= 0
        Send % "{Click Up}"
    Send % "{w Up}"
    Send % "{Ctrl Up}"
        BlockInput, MouseMoveOff
    } else {
        Send % "{Click Down}"
    Send % "{w Down}"
    Send % "{Ctrl Down}"
        BlockInput, MouseMove
    toggle := 1
    If (toggle := 1) 
            SetTimer, loop, -1 
        }
return

loop:
while (toogle := 1) {
    MouseMove, 50, 0, R
    MouseMove, -50, 0, R
    Sleep, 700 
}
return

It presses "left click", "w" and "ctrl" If pressed "z" again it releases them

The loop should move the mouse every 1 second a bit the left and then the same amount back to the right

But it doesnt work

If i press "z" the loop doesnt stop

:=(docs) is used to assign an expression's result, not to compare values.
Use the = or == operators to compare.
Or when you're simply checking true/false values,
(toggle = 1) is the same as (toggle) and
(toggle = 0) is the same as (!toggle) .

Also you misspelled toggle in your while loop.
Here's the the fixed code (it still seems quite broken logic wise).
But maybe you can now fix the logic when the script errors are gone.

z::
    if (GetKeyState("LButton"))
    {
        toggle := 0
        Send % "{Click Up}"
        Send % "{w Up}"
        Send % "{Ctrl Up}"
        BlockInput, MouseMoveOff
    } 
    else 
    {
        Send % "{Click Down}"
        Send % "{w Down}"
        Send % "{Ctrl Down}"
        BlockInput, MouseMove
        toggle := 1
        
        ;this check will never be false because literally right above you set the value?
        if (toggle) 
            SetTimer, loop, -1 
    }
return

loop:
    while (toggle) 
    {
        MouseMove, 50, 0, R
        MouseMove, -50, 0, R
        Sleep, 700 
    }
return

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