Jump to content

Problem with GUI Buttons


Nevalopo
 Share

Recommended Posts

Ok hi all.. Im really new to AutoIT and i need some help with my gui. I am trying to make a bot for a game and i have some problems... here is the code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIconstants.au3>
Opt("GUICoordMode", 1)
GUICreate("Test Title", 300,500)

; Create the controls
$button_1 = GUICtrlCreateButton ("Test 1", 30, 50, 250, 20)
$button_2 = GUICtrlCreateButton ("Test 2", 30, 75, 250, 20)
$group_1 = GUICtrlCreateGroup ("Test info....", 45, 10, 10, 10)
$group_1 = GUICtrlCreateGroup ("Credits: AutoIT", 65, 450, 10, 10)

GUICtrlSetState($button_1, $GUI_FOCUS + $GUI_DEFBUTTON)

GUISetState ()

While 1
   $msg = GUIGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         Exit
         
      Case $msg = $button_1
         MsgBox(0, "Test 1", "Test 1 Started")
            While 1
    ControlSend("botgame", "", "", "{UP}")

    ControlSend("Botgame", "", "", "{DOWN}")
    
    Sleep(600)
    WEnd

          Case $msg = $button_2
         MsgBox(0, "test 2", "test 2 Started")
        While 1
        ControlSend("Botgame", "", "", "hello world")
        ControlSend("Botgame", "", "", "{Enter}")
        Sleep(60000)
        WEnd
   EndSelect
WEnd

Ok so that is my code.. Now the problem is: When i enable test 1 the gui stops working... And i have to shut it down... Or when i enable test 2 i cant enable test 1.... How do i do to make it possible to enable both 1 and 2 ? Right now.. If i enable 1 it just stops... Can anyone please help me out?

Thanks

Link to comment
Share on other sites

  • Moderators

Nevalopo,

First, welcome to the AutoIt forums.

A good start - some code to work on and a clear question. I wish some other newcomers would do the same. :D

Reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here.

In answer to your question, I would use a hotkey to break out of your loops. If you do not do this then you are stuck, because your 2 While..WEnd loops are infinite - that is why you find your GUI becomes unresponsive, it is stuck in the Sisyphean test1/2! :D

Take a look at this - I have commented on my changes so you can see why I have altered what I have:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIconstants.au3>

; Set a hothey to exit the test loops
HotKeySet("{ESC}", "_Exit_Loop")

Opt("GUICoordMode", 1)
GUICreate("Test Title", 300, 500)

; Create the controls
$button_1 = GUICtrlCreateButton("Test 1", 30, 50, 250, 20)
$button_2 = GUICtrlCreateButton("Test 2", 30, 75, 250, 20)
$group_1 = GUICtrlCreateGroup("Test info....", 45, 10, 10, 10)
$group_1 = GUICtrlCreateGroup("Credits: AutoIT", 65, 450, 10, 10)

GUICtrlSetState($button_1, $GUI_FOCUS + $GUI_DEFBUTTON)

GUISetState()

; Set the flag so we keep in the loops at first
$fExit_Loop = False

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit

        Case $msg = $button_1
            MsgBox(0, "Test 1", "Test 1 Started")
            While 1
                ConsoleWrite("UP" & @CRLF); ControlSend("botgame", "", "", "{UP}")
                ConsoleWrite("DOWN" & @CRLF) ;ControlSend("Botgame", "", "", "{DOWN}")

                If $fExit_Loop = True Then ; <<<<<<<<<<<<< test to see if the flag is set
                    $fExit_Loop = False  ; <<<<< reset flag
                    ExitLoop  ; <<<<<<< exit While...WEndloop
                EndIf

                Sleep(600) ; <<<<<<<<<<<<<<<<< this is OK because it is fairly short

            WEnd

        Case $msg = $button_2
            MsgBox(0, "test 2", "test 2 Started")
            While 1
                ConsoleWrite("hello world" & @CRLF) ;ControlSend("Botgame", "", "", "hello world")
                ConsoleWrite("Enter" & @CRLF) ;ControlSend("Botgame", "", "", "{Enter}")

                ;Sleep(60000)  ; <<<<<<<<<<< not good - makes script unresponsive for a whole minute

                ; <<<<<<<<< this is better
                $nBegin = TimerInit() ; start a timer
                Do
                    If $fExit_Loop = True Then ; <<<<<<<<<<<<< test to see if the flag is set
                        $fExit_Loop = False  ; <<<<< reset flag
                        ExitLoop 2  ; <<<<<<< exit both the Do...Until and While...WEnd loops
                    EndIf
                Until TimerDiff($nBegin) > 600  ; keep looping until 600 ms have elapsed - I do not want to wait a whole minute - you can change it back!

            WEnd
    EndSelect
WEnd

Func _Exit_Loop() ; This is called by the hotkey

    $fExit_Loop = True ; <<<<<<<<<<< set flag for exiting loop

EndFunc

Ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Nevalopo,

First, welcome to the AutoIt forums.

A good start - some code to work on and a clear question. I wish some other newcomers would do the same. :D

Reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here.

In answer to your question, I would use a hotkey to break out of your loops. If you do not do this then you are stuck, because your 2 While..WEnd loops are infinite - that is why you find your GUI becomes unresponsive, it is stuck in the Sisyphean test1/2! :D

Take a look at this - I have commented on my changes so you can see why I have altered what I have:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIconstants.au3>

; Set a hothey to exit the test loops
HotKeySet("{ESC}", "_Exit_Loop")

Opt("GUICoordMode", 1)
GUICreate("Test Title", 300, 500)

; Create the controls
$button_1 = GUICtrlCreateButton("Test 1", 30, 50, 250, 20)
$button_2 = GUICtrlCreateButton("Test 2", 30, 75, 250, 20)
$group_1 = GUICtrlCreateGroup("Test info....", 45, 10, 10, 10)
$group_1 = GUICtrlCreateGroup("Credits: AutoIT", 65, 450, 10, 10)

GUICtrlSetState($button_1, $GUI_FOCUS + $GUI_DEFBUTTON)

GUISetState()

; Set the flag so we keep in the loops at first
$fExit_Loop = False

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit

        Case $msg = $button_1
            MsgBox(0, "Test 1", "Test 1 Started")
            While 1
                ConsoleWrite("UP" & @CRLF); ControlSend("botgame", "", "", "{UP}")
                ConsoleWrite("DOWN" & @CRLF) ;ControlSend("Botgame", "", "", "{DOWN}")

                If $fExit_Loop = True Then ; <<<<<<<<<<<<< test to see if the flag is set
                    $fExit_Loop = False  ; <<<<< reset flag
                    ExitLoop  ; <<<<<<< exit While...WEndloop
                EndIf

                Sleep(600) ; <<<<<<<<<<<<<<<<< this is OK because it is fairly short

            WEnd

        Case $msg = $button_2
            MsgBox(0, "test 2", "test 2 Started")
            While 1
                ConsoleWrite("hello world" & @CRLF) ;ControlSend("Botgame", "", "", "hello world")
                ConsoleWrite("Enter" & @CRLF) ;ControlSend("Botgame", "", "", "{Enter}")

                ;Sleep(60000)  ; <<<<<<<<<<< not good - makes script unresponsive for a whole minute

                ; <<<<<<<<< this is better
                $nBegin = TimerInit() ; start a timer
                Do
                    If $fExit_Loop = True Then ; <<<<<<<<<<<<< test to see if the flag is set
                        $fExit_Loop = False  ; <<<<< reset flag
                        ExitLoop 2  ; <<<<<<< exit both the Do...Until and While...WEnd loops
                    EndIf
                Until TimerDiff($nBegin) > 600  ; keep looping until 600 ms have elapsed - I do not want to wait a whole minute - you can change it back!

            WEnd
    EndSelect
WEnd

Func _Exit_Loop() ; This is called by the hotkey

    $fExit_Loop = True ; <<<<<<<<<<< set flag for exiting loop

EndFunc

Ask if anything is unclear.

M23

Thanks for your fast reply and kind words! My problem still remains. I dont understand why you change the

ControlSend("botgame", "", "", "{UP}")

    ControlSend("Botgame", "", "", "{DOWN}")

to

ConsoleWrite("UP" & @CRLF); ControlSend("botgame", "", "", "{UP}")
                ConsoleWrite("DOWN" & @CRLF) ;ControlSend("Botgame", "", "", "{DOWN}")

Seems like that didnt work.

Anyways.. My same problem still remains as it is... I can not turn on both test 1 and test 2 at the same time. The exit hotkey works properly so that is not it... I want to be able to turn on both test 1 and test 2 at the same time. Right now that doesnt work :/ Any more help please?

Thanks

Link to comment
Share on other sites

Here is my proof of concept for responsive loop and responsive sleep

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIconstants.au3>

Opt("GUICoordMode", 1)

GUICreate("Test Title", 300, 500)

; Create the controls
$button_1 = GUICtrlCreateButton("Test 1", 30, 50, 250, 20)
$button_2 = GUICtrlCreateButton("Test 2", 30, 75, 250, 20)
$button_3 = GUICtrlCreateButton("Stop Test", 30, 100, 250, 20)

$label_1 = GUICtrlCreateLabel("", 30, 150, 250, 20)

$group_1 = GUICtrlCreateGroup("Test info....", 45, 10, 10, 10)
$group_1 = GUICtrlCreateGroup("Credits: AutoIT", 65, 450, 10, 10)

GUICtrlSetState($button_1, $GUI_FOCUS + $GUI_DEFBUTTON)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit

        Case $msg = $button_1
;~             MsgBox(0, "Test 1", "Test 1 Started")
            GUICtrlSetData($label_1, "Test 1 Started")
            While 1
;~                 If IsStop() Then ExitLoop
;~                 ControlSend("botgame", "", "", "{UP}")
;~                 ControlSend("Botgame", "", "", "{DOWN}")
                If SleepResponsive(600) Then ExitLoop
            WEnd
            GUICtrlSetData($label_1, "Test 1 Stopped")

    EndSelect
WEnd

Func IsStop()
    If GUIGetMsg() = $button_3 Then Return 1
    Return 0
EndFunc

Func SleepResponsive($time)
    Local $start = TimerInit()
    
    While TimerDiff($start) < $time
        If GUIGetMsg() = $button_3 Then Return 1
    WEnd
    
    Return 0
EndFunc
Edited by Zedna
Link to comment
Share on other sites

  • Moderators

Nevalopo,

I changed the ControlSend lines because I do not have your app to ControlSend to! All you have to do is return them to the way you originally coded them.

I did not realise you wanted to have both tests running at the same time - sorry :D . Try this version:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIconstants.au3>

; Set a hothey to exit the test loop
HotKeySet("{ESC}", "_Exit_Loop")

Opt("GUICoordMode", 1)
GUICreate("Test Title", 300, 500)

; Create the controls
$button_1 = GUICtrlCreateButton("Test 1", 30, 50, 250, 20)
$button_2 = GUICtrlCreateButton("Test 2", 30, 75, 250, 20)
$group_1 = GUICtrlCreateGroup("Test info....", 45, 10, 10, 10)
$group_1 = GUICtrlCreateGroup("Credits: AutoIT", 65, 450, 10, 10)

GUICtrlSetState($button_1,  BitOR($GUI_FOCUS, $GUI_DEFBUTTON)) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Always use BitOR to combine styles

GUISetState()

; Set the flags so we keep in the loop at first, but do not run tests
$fExit_Loop = False
$fTest1 = False
$fTest2 = False
$iCount = 0

While 1

    If $fTest2 = True And $iCount = 10 Then  ; This will give 6000ms delay  - you can alter it to get the delay you want
        ConsoleWrite("hello world" & @CRLF) ;ControlSend("Botgame", "", "", "hello world")
        ConsoleWrite("Enter" & @CRLF) ;ControlSend("Botgame", "", "", "{Enter}")

        $iCount = 0 ; restart count

    EndIf

    If $fTest1 = True Then
        ConsoleWrite("UP" & @CRLF); ControlSend("botgame", "", "", "{UP}")
        ConsoleWrite("DOWN" & @CRLF) ;ControlSend("Botgame", "", "", "{DOWN}")

        $iCount += 1 ; Increase count to get ready for test 2

    EndIf

    $nBegin = TimerInit() ; start a timer
    Do
        If $fExit_Loop = True Then ; <<<<<<<<<<<<< test to see if the flag is set
            $fExit_Loop = False ; <<<<< reset flag
            ExitLoop 2 ; <<<<<<< exit both the Do...Until and While...WEnd loop
        EndIf

        ; Check for any GUI messages
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit

            Case $msg = $button_1
                MsgBox(0, "Test 1", "Test 1 Started")
                $fTest1 = True ; turn on Test 1


            Case $msg = $button_2
                MsgBox(0, "test 2", "test 2 Started")
                $fTest2 = True ; turn on Test 2


        EndSelect

    Until TimerDiff($nBegin) > 600 ; keep looping until 600 ms have elapsed

WEnd

Func _Exit_Loop() ; This is called by the hotkey

    $fExit_Loop = True ; <<<<<<<<<<< set flag for exiting loop
    $fTest1 = False ; <<<<<<<<<<< turn off Test 1
    $fTest2 = False ; <<<<<<<<<<< turn off Test 2

EndFunc   ;==>_Exit_Loop

Again, ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...