Ian_Mac

Button on/off

20 posts in this topic

#1 ·  Posted

hello guys, my script has a button on and of when click, and when its clicked ON the code runs saying get the inputted number and multiply it to 1000 and the msgbox show showing the  answer of the multiplied number.

Now. i wanted to have: a loop that send("0") and sleep($readInput) while the button is ON, and stop the loop when i click the button to OFF but not exiting the whole GUI. 

thank you in advance godbless

Practice.au3

Share this post


Link to post
Share on other sites



#2 ·  Posted

hello guys, my script has a button on and of when click, and when its clicked ON the code runs saying get the inputted number and multiply it to 1000 and the msgbox show showing the  answer of the multiplied number.

Now. i wanted to have: a loop that send("0") and sleep($readInput) while the button is ON, and stop the loop when i click the button to OFF but not exiting the whole GUI. 

thank you in advance godbless

Share this post


Link to post
Share on other sites

#3 ·  Posted

Ian_Mac,

Post your script.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#4 ·  Posted

Ian_Mac,

Post your script, please.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#5 ·  Posted

there in the attached file sir.

 

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

#Region ### START Koda GUI section ### Form=f:\cabal.kxf
$Practice = GUICreate("Practice", 278, 848, 0, 7, $GUI_DOCKSIZE)
GUISetBkColor(0x000000)
WinSetTrans($Practice, "", 190)
$Exit = GUICtrlCreateButton("Exit", 188, 8, 75, 25)
$switch = GUICtrlCreateButton("Off", 8, 8, 75, 25)
   GUICtrlSetBkColor($switch, 0xff0000)
$time = GUICtrlCreateInput("", 82, 10, 30, 21, $ES_NUMBER)
$Enter = GUICtrlCreateButton("Enter", 8, 70, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
         Case $Exit
            Exit
         Case $switch
            onOff()

    EndSwitch
WEnd

;functions for switch
Func onOff()
    If GUICtrlRead($switch) = 'Off' Then
                GUICtrlSetData($switch, 'On')
                GUICtrlSetBkColor($switch, 0x00ff00)
                $readInput = GUICtrlRead($time)
                  If $readInput Then
                    $readInput = $readInput * 1000
                     MsgBox(0, "", "You Inputed: " &$readInput)

                  EndIf

            Else
               $try = GUICtrlSetData($switch, 'Off')
               GUICtrlSetBkColor($switch, 0xff0000)

            EndIf
EndFunc

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ian_Mac,

Please use code tags (see my sig).

How often do you want to run the loop to send "0"?

kylomas

edit: Please explain what you are trying to do.  Sending a "0" to the active window in your code makes no sense as your window is the "active" window.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

15 hours ago, kylomas said:

Ian_Mac,

Please use code tags (see my sig).

How often do you want to run the loop to send "0"?

kylomas

my purpose is  to autopress the zero button every minute depending on the inputted minutes for example if i windowactive the notepad i will type 0 every = minutes inputted. for my project presentation in school. 

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

#Region ### START Koda GUI section ### Form=f:\cabal.kxf
$Practice = GUICreate("Practice", 278, 848, 0, 7, $GUI_DOCKSIZE)
GUISetBkColor(0x000000)
WinSetTrans($Practice, "", 190)
$Exit = GUICtrlCreateButton("Exit", 188, 8, 75, 25)
$switch = GUICtrlCreateButton("Off", 8, 8, 75, 25)
   GUICtrlSetBkColor($switch, 0xff0000)
$time = GUICtrlCreateInput("", 82, 10, 30, 21, $ES_NUMBER)
$Enter = GUICtrlCreateButton("Enter", 8, 70, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
         Case $Exit
            Exit
         Case $switch
            onOff()

    EndSwitch
WEnd

;functions for switch
Func onOff()
    If GUICtrlRead($switch) = 'Off' Then
                GUICtrlSetData($switch, 'On')
                GUICtrlSetBkColor($switch, 0x00ff00)
                $readInput = GUICtrlRead($time)
                  If $readInput Then
                    $readInput = $readInput * 1000
                     MsgBox(0, "", "You Inputed: " &$readInput)

                  EndIf

            Else
               $try = GUICtrlSetData($switch, 'Off')
               GUICtrlSetBkColor($switch, 0xff0000)

            EndIf
EndFunc

 

Edited by Ian_Mac

Share this post


Link to post
Share on other sites

#8 ·  Posted

my purpose is  to autopress the zero button every minute depending on the inputted minutes for example if i windowactive the notepad i will type 0 every = minutes inputted. for my project presentation in school. 

lets just say that when i windowactive "notepad" send 0 to it every sec. defending how many minutes inputted in the input box.

Share this post


Link to post
Share on other sites

#9 ·  Posted

to bad no one can answer this problem

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

@Ian_Mac Please do not bump your posts without waiting 24 hours. This may be the most important thing in the world to you right now, but you need to exercise some patience. Forum questions are answered by volunteers on their own time, from every timezone across the globe - the person best able to answer your question may not be on right now.

Edit: and when you do bump your post, try adding something a little more mature like "I've tried x to figure this out on my own, but ran into error y", rather than a snarky little "too bad no one can answer" comment. You'll find forum members more willing to help if you are showing some effort on your own.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#11 ·  Posted

ohh im very sorry, its just that i really want to have a solve to my problem. hope somebody can  help me soon . 

Share this post


Link to post
Share on other sites

#12 ·  Posted

Yes I see that it is for a "school project", I will point out again that we try to give suggestions on what you can try on your own - rather than simply doing it for you. Wouldn't teach you much if someone just wrote the code for you and you didn't understand now would it :)

Based on your code, if you are looking to send a button every x minutes, what have you tried? Send or ControlSend are the obvious choices.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#13 ·  Posted

my problem now is how can i pause or stop the loop when i turn off the button with out exiting the entire GUI. because i tried  use exit or exit loop, it exits the entire gui. 

anway i can right .

func start()
    wihle 1
        send("0")
        sleep($readInput)
    wend
Endfunc

but i dont know how to interrupt the loop anymore. i need it to be turnd off if i button to off/ 

Share this post


Link to post
Share on other sites

#14 ·  Posted

So do you want a sort of pause function? That can be done this way:

HotKeySet("{F8}", "Pause")

Func Pause()
    Local Static $bPaused = False
    $bPaused = Not $bPaused
    While $bPaused
        Sleep(333)
    WEnd
EndFunc   ;==>_Pause

Or if you don't want it to be a hotkey you can just call the function once to pause, and then call it again to unpause.


UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

#15 ·  Posted

23 hours ago, anthonyjr2 said:

So do you want a sort of pause function? That can be done this way:

HotKeySet("{F8}", "Pause")

Func Pause()
    Local Static $bPaused = False
    $bPaused = Not $bPaused
    While $bPaused
        Sleep(333)
    WEnd
EndFunc   ;==>_Pause

Or if you don't want it to be a hotkey you can just call the function once to pause, and then call it again to unpause.

 here's the code i tried and i temporarily  made enter button to be a stop button. but i didn't work. the button is still not clickable. 

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



#Region ### START Koda GUI section ### Form=f:\cabal.kxf
$Practice = GUICreate("Practice", 278, 848, 0, 7, $GUI_DOCKSIZE)
GUISetBkColor(0x000000)
WinSetTrans($Practice, "", 190)
$Exit = GUICtrlCreateButton("Exit", 188, 8, 75, 25)
$switch = GUICtrlCreateButton("Off", 8, 8, 75, 25)
   GUICtrlSetBkColor($switch, 0xff0000)
$time = GUICtrlCreateInput("", 82, 10, 30, 21, $ES_NUMBER)
$StopScript = GUICtrlCreateButton("StopRunning", 8, 70, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
         Case $Exit
            Exit
         Case $switch
            onOff()
         Case $StopScript
            Pause()

    EndSwitch
WEnd


;functions for switch
Func onOff()
    If GUICtrlRead($switch) = 'Off' Then
                GUICtrlSetData($switch, 'On')
                GUICtrlSetBkColor($switch, 0x00ff00)
                $readInput = GUICtrlRead($time)
                  If $readInput Then
                    $readInput = $readInput * 1000
                     MsgBox(0, "", "You Inputed: " &$readInput)
                     While 1            ; this is the part  i wanted to stop but not closing the GUI
                        Send("0")
                        Sleep($ReadInput)
                     WEnd   ; end of the while loop for the sendkey
                  Else
                     MsgBox(0, "", "Wrong Input")
                  EndIf
            Else
               GUICtrlSetData($switch, 'Off')
               GUICtrlSetBkColor($switch, 0xff0000)
            EndIf
EndFunc


Func Pause()
    Local Static $bPaused = False
    $bPaused = Not $bPaused
    While $bPaused
        Sleep(333)
    WEnd
 EndFunc   ;==>_Pause

 

Share this post


Link to post
Share on other sites

#16 ·  Posted

How about this?

 

Local $bBreak = False
 While 1            ; this is the part  i wanted to stop but not closing the GUI
    Send("0")
    For $i = 0 To ($readInput / 100)
        Sleep(100)
        If GUIGetMsg() = $StopScript Then
            GUICtrlSetData($switch, 'Off')
            GUICtrlSetBkColor($switch, 0xff0000)
            $bBreak = True
        EndIf
        If $bBreak = True Then ExitLoop
    Next
    If $bBreak = True Then ExitLoop
 WEnd   ; end of the while loop for the sendkey

 

1 person likes this

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

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



#Region ### START Koda GUI section ### Form=f:\cabal.kxf
$Practice = GUICreate("Practice", 278, 848, 0, 7, $GUI_DOCKSIZE)
GUISetBkColor(0x000000)
WinSetTrans($Practice, "", 190)
$Exit = GUICtrlCreateButton("Exit", 188, 8, 75, 25)
$switch = GUICtrlCreateButton("Off", 8, 8, 75, 25)
   GUICtrlSetBkColor($switch, 0xff0000)
$time = GUICtrlCreateInput("", 82, 10, 30, 21, $ES_NUMBER)
$StopScript = GUICtrlCreateButton("StopRunning", 8, 70, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
         Case $Exit
            Exit
         Case $switch
            onOff()

         Case $StopScript
            Pause()


    EndSwitch
WEnd


;functions for switch
Func onOff()
    If GUICtrlRead($switch) = 'Off' Then
                GUICtrlSetData($switch, 'On')
                GUICtrlSetBkColor($switch, 0x00ff00)
                $readInput = GUICtrlRead($time)
                  If $readInput Then
                    $readInput = $readInput * 1000
                     MsgBox(0, "", "You Inputed: " &$readInput)
                     Local $bBreak = False
                     While 1            ; this is the part  i wanted to stop but not closing the GUI
                        Send("0")
                        For $i = 0 To ($readInput / 100)
                           Sleep(100)
                           If GUIGetMsg() = $StopScript Then
                              GUICtrlSetData($switch, 'Off')
                              GUICtrlSetBkColor($switch, 0xff0000)
                              $bBreak = True
                            EndIf
                              If $bBreak = True Then ExitLoop
                           Next
                           If $bBreak = True Then ExitLoop
                     WEnd   ; end of the while loop for the sendkey
                  EndIf
            Else
               GUICtrlSetData($switch, 'Off')
               GUICtrlSetBkColor($switch, 0xff0000)
            EndIf
EndFunc


Func Pause()
    Local Static $bPaused = False
    $bPaused = Not $bPaused
    While $bPaused
        Sleep(333)
    WEnd
 EndFunc   ;==>_Pause

it works Man!! Thank you so much. you save me.

but i want to understand the code :)

    For $i = 0 To ($readInput / 100) << what does the code says? what 100 stands for?

                           EndIf
                              If $bBreak = True Then ExitLoop
                           Next
                               If $bBreak = True Then ExitLoop  <<<< and why need to make "next" then same line of code? 

Edited by Ian_Mac

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

26 minutes ago, Ian_Mac said:

what does the code says? what 100 stands for?

Basically I am splitting your sleep interval into smaller bits so that the script can check if the button was clicked while waiting. 5000 / 100 = 50 so we have 50 loop-iterations. Each time the loop is iterated through the script waits 100 milliseconds (Sleep(100)). 50 * 100 = 5000 so we are back at the interval that we want.

 

26 minutes ago, Ian_Mac said:

and why need to make "next" then same line of code? 

The first line exits the for-loop and the second line exits the while-loop. I think you could write "ExitLoop 2" and it would exit both loops with only one line of code but I am not sure.

Edited by Floops

Share this post


Link to post
Share on other sites

#19 ·  Posted

thank you man.  you helped me alot.

Share this post


Link to post
Share on other sites

#20 ·  Posted

You're welcome :)

Share this post


Link to post
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