Jump to content

GUI Button Press Detection in another Function?


Recommended Posts

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("MyCommands", 406, 236, 610, 129)
$Main = GUICtrlCreateTab(8, 8, 385, 217)

$TabSheet1 = GUICtrlCreateTabItem("Main")
$StopFunc = GUICtrlCreateButton("STOP", 17, 43, 150, 50)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$StartFunc = GUICtrlCreateButton("Start", 17, 98, 150, 50)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Input0 = GUICtrlCreateInput("Input0", 267, 43, 75, 24, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$Input1 = GUICtrlCreateInput("Input1"", 267, 73, 75, 24, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$Input2 = GUICtrlCreateInput("Input2", 267, 103, 75, 24, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$Input3 = GUICtrlCreateInput("Input3", 267, 133, 75, 24, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$Input4 = GUICtrlCreateInput("Input4", 267, 163, 75, 24, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
$Input5 = GUICtrlCreateInput("Input5", 267, 195, 75, 24, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))

$Label1 = GUICtrlCreateLabel("Label1", 215, 43, 50, 20, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Label2", 215, 73, 50, 20, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("Label3", 215, 103, 50, 20, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$Label4 = GUICtrlCreateLabel("Label4", 215, 133, 50, 20, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$Label5 = GUICtrlCreateLabel("Label5", 215, 163, 50, 20, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$Label6 = GUICtrlCreateLabel("Label6", 191, 195, 74, 20, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")

$TabSheet2 = GUICtrlCreateTabItem("Individuals")
$Button1 = GUICtrlCreateButton("Button1", 17, 43, 75, 25)

GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $StartBOT
            letsdothis()
        Case $StopBOT
            END_This()
    EndSwitch
WEnd

func letsdothis()
    While 1
        ;doing stuff
        ;doing more stuff
        if $StopFunc Then END_This() ;how do i detect if the button has been pressed
        if $Button1 Then Sleepy_Time()
        ;doing things
        ;doing more things
    WEnd
EndFunc

Func Sleepy_Time()
    while 1
        sleep(10)
        if $Button1 Then ExitLoop ; detect button press and return to previous state
    Wend
EndFunc

Func END_This()
    Exit
EndFunc

For the LIFE of me I can't figure out, HOW ON EARTH to detect that a button has been pressed while a function is running?

Can someone PLEASE point me in the right direction???

Thanks

Link to comment
Share on other sites

  • Moderators

Yirrlaar,

The Interrupting a running function tutorial in the Wiki should help lift the fog.

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

Melba23, I looked at the code and came up with this .... but something still is not right ... just a TOUCH of additional assistance would be greatly appreciated. I am having a problem returning to StartUp() after the Pause button is hit again

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $StartButton = 0
Global $PauseButton = 0
Global $StopButton = 0

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 250, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$Start = GUICtrlCreateButton("START", 10, 10, 80, 30)
GUICtrlSetOnEvent($Start, "StartUp")
$Pause = GUICtrlCreateButton("PAUSE", 10, 50, 80, 30)
GUICtrlSetOnEvent($Pause, "PauseThis")
$Stop = GUICtrlCreateButton("STOP", 10, 90, 80, 30)
GUICtrlSetOnEvent($Stop, "STOPThis")

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10)
WEnd

Func _Interrupt_Sleep($iDelay)
    Local $iBegin = TimerInit()
    Do
        Sleep(10)
        If $StartButton Then
            Return "Start"
        elseif $PauseButton Then
            Return "Pause"
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return False
EndFunc   ;==>_Interrupt_Sleep

Func StartUp()
    $StartButton = 0
    For $i = 1 To 100
        ConsoleWrite("-StartUp Running" & @CRLF)
        local $testing = _Interrupt_Sleep(5000)
        If $testing = "Start" Then
            Switch $StartButton
                Case 1
                    ConsoleWrite("!StartUp interrrupted by Pause" & @CRLF)
                    PauseThis()
                Case 2
                    ConsoleWrite("!StartUp interrrupted by STOP" & @CRLF)
                    STOPThis()
            EndSwitch
            Return
        EndIf
        Sleep(100)
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
EndFunc   ;==>StartUp

Func PauseThis()
    $PauseButton = 0
    For $i = 1 To 100
        ConsoleWrite("-PauseThis Running" & @CRLF)
        local $testing = _Interrupt_Sleep(5000)
        If $testing = "Pause" Then
            Switch $PauseButton
                Case 1
                    ConsoleWrite("!PauseThis interrrupted by PauseThis" & @CRLF)
                Case 2
                    ConsoleWrite("!PauseThis interrrupted by Start" & @CRLF)
                    Return
                Case 3
                    ConsoleWrite("!PauseThis Stop" & @CRLF)
                    STOPThis()
            EndSwitch
            Return
        EndIf
        Sleep(100)
    Next
    ConsoleWrite(">PauseThis Ended" & @CRLF)
EndFunc   ;==>PauseThis

Func STOPThis()
    _Exit()
EndFunc   ;==>STOPThis

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Func 2 button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) = $Start Then
        $StartButton = 0
        $PauseButton = 1
    EndIf
    If BitAND($wParam, 0x0000FFFF) = $Pause Then
        $StartButton = 1
        $PauseButton = 1
    EndIf
    If BitAND($wParam, 0x0000FFFF) = $Stop Then
        $StartButton = 2
        $PauseButton = 2
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

 

Output:

Quote

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-PauseThis Running
-PauseThis Running
-PauseThis Running
-PauseThis Running
!PauseThis interrrupted by Start (Hit the start Button but PauseThis continues to run)
-PauseThis Running
-PauseThis Running
-PauseThis Running
-PauseThis Running
!PauseThis interrrupted by Start (Hit the start Button AGAIN and finally returns)
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running
-StartUp Running

Edited by Yirrlaar
Link to comment
Share on other sites

  • Moderators

Yirrlaar,

How about this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $StartButton = 0
Global $PauseButton = 0
Global $StopButton = 0

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 250, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$Start = GUICtrlCreateButton("START", 10, 10, 80, 30)
GUICtrlSetOnEvent($Start, "StartUp")
$Pause = GUICtrlCreateButton("PAUSE", 10, 50, 80, 30)
GUICtrlSetOnEvent($Pause, "PauseThis")
$Stop = GUICtrlCreateButton("STOP", 10, 90, 80, 30)
GUICtrlSetOnEvent($Stop, "STOPThis")

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10)
WEnd

Func _Interrupt_Sleep($iDelay)
    Local $iBegin = TimerInit()
    Do
        Sleep(10)
        If $StopButton Then
            Return "Stop"
        ElseIf $PauseButton Then
            Return "Pause"
        ElseIf $StartButton Then
            Return "Start"
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return ""
EndFunc   ;==>_Interrupt_Sleep

Func StartUp()
    $StartButton = 0
    For $i = 1 To 100
        ConsoleWrite("-StartUp Running - " & $i & @CRLF)

        Switch _Interrupt_Sleep(1000)
            Case "Stop"
                $StopButton = 0
                ConsoleWrite("!StartUp interrrupted by STOP" & @CRLF)
                STOPThis()
            Case "Pause"
                $PauseButton = 0
                ConsoleWrite("!StartUp interrrupted by Pause" & @CRLF)
                PauseThis()
        EndSwitch
        Sleep(100)
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
EndFunc   ;==>StartUp

Func PauseThis()
    $PauseButton = 0
    For $i = 1 To 100
        ConsoleWrite("-PauseThis Running - " & $i & @CRLF)
        Switch _Interrupt_Sleep(1000)
            Case "Stop"
                $StopButton = 0
                ConsoleWrite("-PauseThis interrrupted by STOP" & @CRLF)
                STOPThis()
            Case "Start"
                $StartButton = 0
                ConsoleWrite("!StartUp interrrupted by Pause" & @CRLF)
                Return
        EndSwitch
        Sleep(100)
    Next
    ConsoleWrite(">PauseThis Ended" & @CRLF)
EndFunc   ;==>PauseThis

Func STOPThis()
    _Exit()
EndFunc   ;==>STOPThis

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $Start Then
        $StartButton = 1
    EndIf
    If BitAND($wParam, 0x0000FFFF) = $Pause Then
        $PauseButton = 1
    EndIf
    If BitAND($wParam, 0x0000FFFF) = $Stop Then
        $StopButton = 1
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

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

12 hours ago, Melba23 said:

Yirrlaar,

How about this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $StartButton = 0
Global $PauseButton = 0
Global $StopButton = 0

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 250, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$Start = GUICtrlCreateButton("START", 10, 10, 80, 30)
GUICtrlSetOnEvent($Start, "StartUp")
$Pause = GUICtrlCreateButton("PAUSE", 10, 50, 80, 30)
GUICtrlSetOnEvent($Pause, "PauseThis")
$Stop = GUICtrlCreateButton("STOP", 10, 90, 80, 30)
GUICtrlSetOnEvent($Stop, "STOPThis")

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10)
WEnd

Func _Interrupt_Sleep($iDelay)
    Local $iBegin = TimerInit()
    Do
        Sleep(10)
        If $StopButton Then
            Return "Stop"
        ElseIf $PauseButton Then
            Return "Pause"
        ElseIf $StartButton Then
            Return "Start"
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return ""
EndFunc   ;==>_Interrupt_Sleep

Func StartUp()
    $StartButton = 0
    For $i = 1 To 100
        ConsoleWrite("-StartUp Running - " & $i & @CRLF)

        Switch _Interrupt_Sleep(1000)
            Case "Stop"
                $StopButton = 0
                ConsoleWrite("!StartUp interrrupted by STOP" & @CRLF)
                STOPThis()
            Case "Pause"
                $PauseButton = 0
                ConsoleWrite("!StartUp interrrupted by Pause" & @CRLF)
                PauseThis()
        EndSwitch
        Sleep(100)
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
EndFunc   ;==>StartUp

Func PauseThis()
    $PauseButton = 0
    For $i = 1 To 100
        ConsoleWrite("-PauseThis Running - " & $i & @CRLF)
        Switch _Interrupt_Sleep(1000)
            Case "Stop"
                $StopButton = 0
                ConsoleWrite("-PauseThis interrrupted by STOP" & @CRLF)
                STOPThis()
            Case "Start"
                $StartButton = 0
                ConsoleWrite("!StartUp interrrupted by Pause" & @CRLF)
                Return
        EndSwitch
        Sleep(100)
    Next
    ConsoleWrite(">PauseThis Ended" & @CRLF)
EndFunc   ;==>PauseThis

Func STOPThis()
    _Exit()
EndFunc   ;==>STOPThis

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $Start Then
        $StartButton = 1
    EndIf
    If BitAND($wParam, 0x0000FFFF) = $Pause Then
        $PauseButton = 1
    EndIf
    If BitAND($wParam, 0x0000FFFF) = $Stop Then
        $StopButton = 1
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

M23

Melba23, you are the BEST but I made 1 very very VERY TINY tweak to make it work 100% with when the pause button KEEPS getting pressed .... 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $StartButton = 0
Global $PauseButton = 0
Global $StopButton = 0

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 250, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "STOPThis")

$Start = GUICtrlCreateButton("START", 10, 10, 80, 30)
GUICtrlSetOnEvent($Start, "StartUp")
$Pause = GUICtrlCreateButton("PAUSE", 10, 50, 80, 30)
GUICtrlSetOnEvent($Pause, "PauseThis")
$Stop = GUICtrlCreateButton("STOP", 10, 90, 80, 30)
GUICtrlSetOnEvent($Stop, "STOPThis")

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10)
WEnd

Func _Interrupt_Sleep($iDelay)
    Local $iBegin = TimerInit()
    Do
        Sleep(10)
        If $StopButton Then
            Return "Stop"
        ElseIf $PauseButton Then
            Return "Pause"
        ElseIf $StartButton Then
            Return "Start"
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return ""
EndFunc   ;==>_Interrupt_Sleep

Func StartUp()
    $StartButton = 0
    For $i = 1 To 100
        ConsoleWrite("-StartUp Running - " & $i & @CRLF)

        Switch _Interrupt_Sleep(1000)
            Case "Stop"
                $StopButton = 0
                ConsoleWrite("!StartUp interrrupted by STOP" & @CRLF)
                STOPThis()
            Case "Pause"
                $PauseButton = 0
                ConsoleWrite("!StartUp interrrupted by Pause" & @CRLF)
                PauseThis()
        EndSwitch
        Sleep(100)
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
EndFunc   ;==>StartUp

Func PauseThis()
    $PauseButton = 0
    For $i = 1 To 100
        ConsoleWrite("-PauseThis Running - " & $i & @CRLF)
        Switch _Interrupt_Sleep(1000)
            Case "Stop"
                $StopButton = 0
                ConsoleWrite("-PauseThis interrrupted by STOP" & @CRLF)
                STOPThis()
            Case "Start"
                $StartButton = 0
                ConsoleWrite("!PauseThis interrrupted by Start" & @CRLF)
                Return
            Case "Pause"
                $PauseButton = 0
                ConsoleWrite("!PauseThis interrrupted by Pause" & @CRLF)
                Return
        EndSwitch
        Sleep(100)
    Next
    ConsoleWrite(">PauseThis Ended" & @CRLF)
EndFunc   ;==>PauseThis

Func STOPThis()
    Exit
EndFunc   ;==>STOPThis

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $Start Then $StartButton = 1
    If BitAND($wParam, 0x0000FFFF) = $Pause Then $PauseButton = 1
    If BitAND($wParam, 0x0000FFFF) = $Stop Then $StopButton = 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

 

Output:

Quote

-StartUp Running - 1
-StartUp Running - 2
!StartUp interrrupted by Pause
-PauseThis Running - 1
-PauseThis Running - 2
-PauseThis Running - 3
!PauseThis interrrupted by Pause
-StartUp Running - 3
-StartUp Running - 4
-StartUp Running - 5
!StartUp interrrupted by Pause
-PauseThis Running - 1
-PauseThis Running - 2
-PauseThis Running - 3
-PauseThis interrrupted by STOP

 

Edited by Yirrlaar
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

×
×
  • Create New...