loopsuser-interfaceautohotkeyautohotkey-2

How do I make a GUI which asks the user how many loops the script should do in AutoHotkey V2


I want to make a Program which asks the user how many times the inner loop should loop. It should also ask only once and then run indefinitely with the number of loops the user set.

Here is the code:

s::{
    loop {
        ; This loop runs indefinitely
        Loop numLoops {
            ; The user-defined inner loop
            MouseMove 64, 352
            Click
            Send "{Delete}"
            Sleep 100
            Send "{Enter}"
            Sleep 500
        }

The Script runs fine, but my problem is that you need to change the amount of loops in the code itself, which is not very good. This autoclicker script is for work, and most of my colleagues have no idea what AutoHotkey even is. So I want to make a GUI for them where they can input how many loops the program does.

I read the AutoHotkey V2 documentation extensively (here is the link to the documentation), but I don't understand it well enough to apply the knowledge into my program.

Here is what I already tried:

global numLoops := 0 ; Variable to store the number of loops

class GUI extends Object
; Create a GUI to prompt for the number of loops for the inner loop


; Wait until the user has input the number of loops before continuing
while (numLoops = 0) {
    Sleep 100 ; Wait until the user provides input
}

LoopGUI := Gui()
LoopGUI := Gui.Call()

NumberLoopsGUI := LoopGUI.Call()
NumberLoopsGUI.Add("Text", "Please enter the number of inner loops:")

s::{
    loop {
        ; This loop runs indefinitely
        Loop numLoops {
            ; The user-defined inner loop
            MouseMove 64, 352
            Click
            Send "{Delete}"
            Sleep 100
            Send "{Enter}"
            Sleep 500
        }

Where do I put my $global numLoops and how does the $Gui.Call() operator work?


Solution

  • Its sometimes a pain to work with gui controls but trust me they are easier than you think. A control has a vControl value which allows you to access the control from the gui itself. Syntax is easier than it sounds: GUI_Object["myControlString"].Value

    Then the updated script is:

    LoopGUI := Gui()
    
    /*
    First parameter is the control type we are creating
    Using "v" is required but you can use any name after the initial "v"
    x y w and h are coordinates and size
    last parameter is the default value to start with
    */
    LoopGUI.Add("Edit", "x140 y16 w50 h21 +Number -VScroll vCtrl_numLoops","1") ;Use the Number flag to only accept natural numbers
    LoopGUI.Add("Text","x16 y16 w120 h40" ,"Please enter the number of inner loops:")
    
    LoopGUI.Show("w200 h70")
    
    s::{
        Loop {
            ; This loop runs indefinitely
            Loop Integer(LoopGUI["Ctrl_numLoops"].Value) { ;Directly use the loop count from the gui control
                ; The user-defined inner loop
                MouseClick("Left",64,352)
                Send "{Delete}"
                Sleep 100
                Send "{Enter}"
                Sleep 500
            }
            MsgBox("Close this window to execute the inner loop again.")
        }
    }