Jump to content

NEWBIE Q: End long loop in gui with button


Jfish
 Share

Recommended Posts

I am a super-newbie that is trying to learn - so I apologize for the simplicity of this question. I wanted to write a small app to keep my desktop "awake" as I don't have access to the screensaver controls that come on every 10 minutes. Accordingly, I created a simple GUI with two buttons that start and stop a loop that sends the shift key every thirty seconds. My problem is that once the first button is pressed and the loop begins, I cannot stop it with the second button. I don't understand why the GUI will not listen for $Button2 during a sleep cycle. Can somebody help me with this admittedly newb question? I thank you in advance for your tutelage.

; send shift keystrokes to stop screensaver from starting in windows

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=

$AForm1_1 = GUICreate("KeySender v1.0", 390, 155, 194, 126)

GUISetBkColor(0x0C5490)

$Button1 = GUICtrlCreateButton("Start", 80, 80, 113, 25)

$Button2 = GUICtrlCreateButton("Stop", 208, 80, 97, 25)

$Label1 = GUICtrlCreateLabel("Will send SHIFT keystroke every 30 seconds for 10,000 loops until stopped", 16, 16, 359, 17)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

Func startIt()

MsgBox("","KeySender 1.0", "STARTING - will send SHIFT key every 30 seconds 10,000 times")

dim $a = 1

dim $loops = 10000 ; 10,000 loops

for $a = 1 To 10000 step 1

send("{LSHIFT}")

;MsgBox("","confirmation","Shift Sent")

sleep(30000)

Next

EndFunc

Func stopIt()

Exit

EndFunc

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Button1

startIt()

Case $Button2

stopIt()

EndSwitch

WEnd

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Maybe...

#include <GUIConstants.au3>

Dim $Looper = 0, $lCount = 0

#Region ### START Koda GUI section ### Form=
$AForm1_1 = GUICreate("KeySender v1.0", 390, 155, 194, 126)
GUISetBkColor(0x0C5490)
$Button1 = GUICtrlCreateButton("Start", 80, 80, 113, 25)
$Button2 = GUICtrlCreateButton("Stop", 208, 80, 97, 25)
$Label1 = GUICtrlCreateLabel("Will send SHIFT keystroke every 30 seconds for 10,000 loops until stopped", 16, 16, 359, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            MsgBox("", "KeySender 1.0", "STARTING - will send SHIFT key every 30 seconds 10,000 times", 2)
            $Looper = 0
            $lCount = 0
            AdlibEnable("Sender", 3000) ;**************** 30000 ??? **********
        Case $Button2
            $Looper = 0
            $lCount = 0
            AdlibDisable()
    EndSwitch
    
WEnd


Func Sender()
    $lCount = $lCount + 1
    If $lCount >= 10000 Then
        $Looper = 0
        $lCount = 0
        AdlibDisable()
        Return
    EndIf
    ;Send("{LSHIFT}")
    MsgBox(0x0, "Test", $lCount, 2)
EndFunc

8)

NEWHeader1.png

Link to comment
Share on other sites

added a "stop" message box to show it is working

#include <GUIConstants.au3>

Dim $Looper = 0, $lCount = 0

#Region ### START Koda GUI section ### Form=
$AForm1_1 = GUICreate("KeySender v1.0", 390, 155, 194, 126)
GUISetBkColor(0x0C5490)
$Button1 = GUICtrlCreateButton("Start", 80, 80, 113, 25)
$Button2 = GUICtrlCreateButton("Stop", 208, 80, 97, 25)
$Label1 = GUICtrlCreateLabel("Will send SHIFT keystroke every 30 seconds for 10,000 loops until stopped", 16, 16, 359, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            MsgBox("", "KeySender 1.0", "STARTING - will send SHIFT key every 30 seconds 10,000 times", 2)
            $Looper = 0
            $lCount = 0
            AdlibEnable("Sender", 3000) ;**************** 30000 ??? **********
        Case $Button2
            $Looper = 0
            $lCount = 0
            AdlibDisable()
            MsgBox(0x0, "Test", "STOPED", 2) ; added for testing
    EndSwitch
    
WEnd


Func Sender()
    $lCount = $lCount + 1
    If $lCount >= 10000 Then
        $Looper = 0
        $lCount = 0
        AdlibDisable()
        Return
    EndIf
    ;Send("{LSHIFT}")
    MsgBox(0x0, "Test", $lCount, 2)
EndFunc

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Thank you very much for the help. It looks like you took it out of the FOR loop that I was using and called a function that will increment at a specificied time interval, send the key, then, if it hits 10,000 cycles it will reset the count and start over. Very cool.

I was not familiar with how to use the adlib function - that is a very nice example. I really appreciate your taking the time to help me out.

:whistle:

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...