Autohotkey unable to exit loop on keypress (Writing Global Variables)

25 Views Asked by At

I am creating a simple repeat keystroke loop that is supposed to break on pressing ^F2. I have tried debugging but am unable to. I do get the prompt when I press ^F2, indicating that it was registered. In another debugging attempt, it seems like the global variable "checkbit" is not being changed. Any idea why? I am not familiar with how AHK variables work...

global checkbit := 0

^F1::{
checkbit := 0
Loop{
    Send "{Enter}"
    Sleep 1000
    if checkbit = 1{
        msgbox "exiting loop"
        break
    }
}
return
}


^F2::{
    msgbox "exiting"
    checkbit := 1
    return
}

I tried more debugging and found out that the variables inside my ^F1 function are local and does not read the global variable. How do I resolve this? I've seen other similar threads that apparently works but I replicated those and failed

1

There are 1 best solutions below

0
Altron Lee On

Upon further research, I have found a useful thread about writing global variables... https://www.autohotkey.com/boards/viewtopic.php?t=112514

global checkbit := 0

^F1::{
    global checkbit:=0
    Loop{
    Send "{Enter}"
    Sleep 1000
    if checkbit = 1{
        msgbox "exiting loop"
        break
    }
}
return
}


^F2::{
    global checkbit
    msgbox "exiting"
    checkbit := 1
    return
}

so we gotta declare "global" within the function to write the global variable. This works now!