Jump to content
Sign in to follow this  
cording12

Simple GUI Script that I can't fix :S

Recommended Posts

cording12

Hi there,

I'm using this to help me with a college project and need to use the handy GUI feature. So far I got it to open the GUI and be able to input the key you want pressed and the delay for said key.

The issue to start with was it would work fine after I hit 'Start' but then the script wouldn't stop at all or close when pressing the windows close X on the window.

I jiggled it around a lot and added a stop button which I can't seem to get working and now all of a sudden when pressing start that doesn't do anything either!

I've spent hours on this reading all the helps and Forums possible before I got a headache so would love some help here!

Thank you all!

#cs ----------------------------------------------------------------------------
cording12
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
GUICreate("Cording'st", 335, 100)
GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
GUICtrlSetOnEvent($startbutton, "ButtonPressed")
$stopbutton = GUICtrlCreateButton("Stop", 190, 40, 60)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitProgram")
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()

Select

  Case $msg = $startbutton
  
    $send1 = GUICtrlRead($key1)
    $sleep1 = GUICtrlRead($time1)
  
   While 1
    Send($send1)
    Sleep($sleep1)
   WEnd
  

  
EndSelect

WEnd
Func ExitProgram ()
    Exit
EndFunc   ;==>ExitProgram

Share this post


Link to post
Share on other sites
Melba23

cording12,

Welcome to the AutoIt forum. ;)

You are mixing OnEvent and MessageLoop mode in the same script - at your stage you need to use one or the other. :)

And I notice that you spamming keys with this script - you have read the Forum Rules I hope. :)

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

 

Share this post


Link to post
Share on other sites
cording12

Hey Melba23 thanks for the reply,

I have indeed read the rules and this script is designed to be used to automate the pressing of my keyboard macro keys every couple minutes and nothing else!

Ok so I should take out the Get Msg?

Share this post


Link to post
Share on other sites
cording12

I've tried looking in the help files and can't find anything that will help! Please can you say where in the help files to look roughly or if there is something in particular that is causing it to mess up?

Thanks

Share this post


Link to post
Share on other sites
Melba23

cording12,

I have already told you what is wrong with your script:

You are mixing OnEvent and MessageLoop mode in the same script - [...] you need to use one or the other

Look in the Help file under <Gui Reference> and scroll down to the <GUI Event Modes> section - this will explain the difference the 2 modes and provides links to more comprehensive explanations. :)

If you still have trouble understanding where you are going wrong after having read these sections carefully, come back and ask again. ;)

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

 

Share this post


Link to post
Share on other sites
cording12

Hey there,

Thanks for the replies Melba23 every bit of information you give is a total gold nugget! I just spent the past two hours reading all the GUI On Event stuff and am absolutely stumped. Maybe I'm just exhausted but I cannot see how to implement it using GUI Event modes!!

This is what I got to so far and I have a feeling I may be on the right track (hopefully!)

#cs ----------------------------------------------------------------------------
cording12
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
;
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
;
GUICreate("Test Script", 335, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
;
GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
GUICtrlSetOnEvent($key1, "KeyButton")
;
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
GUICtrlSetOnEvent($time11, "TimeButton")
;
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
GUICtrlSetOnEvent($startbutton, "StartButtonPressed")
;
$stopbutton = GUICtrlCreateButton("Stop", 190, 40, 60)
GUICtrlSetOnEvent = ($stopbutton, "StopButtonPressed")
;
GUISetState(@SW_SHOW)
;
While 1
  Sleep(1000)  ; Idle around
WEnd
;
Func StartButtonPressed ()
$send1 = GUICtrlRead ($key1)
$sleep1 = GUICtrlRead ($time1)
EndFunc
;
Func StopButtonPressed
MsgBox (0, "Stop", "Script Stopped. Now Exiting"
Exit
EndFunc

Share this post


Link to post
Share on other sites
Melba23

cording12,

Does this help you to understand how you set up the OnEvent functions: ;)

#include <GUIConstantsEx.au3>
;
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
;
GUICreate("Test Script", 335, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Run this function when the [X] is clicked
;
GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
; No need to set a function for this control as it is not actioned
;
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
; No need to set a function for this control as it is not actioned
;
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
GUICtrlSetOnEvent($startbutton, "StartButtonPressed") ; Run this function when the button is pressed
;
$stopbutton = GUICtrlCreateButton("Stop", 190, 40, 60)
GUICtrlSetOnEvent($stopbutton, "StopButtonPressed") ; Run this function when the button is pressed
;
GUISetState(@SW_SHOW)
;
While 1
    Sleep(1000) ; Idle around
WEnd

; This function is run when the [X] is clicked
Func CLOSEClicked()
    MsgBox(0, "Stop", "GUI closed. Now Exiting")
    Exit
EndFunc   ;==>CLOSEClicked
;
; This function is run when the start button is pressed
Func StartButtonPressed()
    $send1 = GUICtrlRead($key1)
    $sleep1 = GUICtrlRead($time1)
    MsgBox(0, "Read", "Key: " & $send1 & @CRLF & "Sleep: " & $sleep1)
EndFunc   ;==>StartButtonPressed
;
; This function is run when the stop button is pressed
Func StopButtonPressed()
    MsgBox(0, "Stop", "Script Stopped. Now Exiting")
    Exit
EndFunc   ;==>StopButtonPressed

There were also plenty of syntax errors in that script:

GUICtrlSetOnEvent = ($stopbutton, "StopButtonPressed") ; Why the = ?
Func StopButtonPressed                                 ; Where are the () ?
MsgBox (0, "Stop", "Script Stopped. Now Exiting"       ; No final )

You need to check that the syntax is correct or you will never get code to run. :)

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

 

Share this post


Link to post
Share on other sites
cording12

Hey there,

Thats a massive help so thanks a lot!

Only is issue is when you press start it doesn't actually press the keys. I tested on notepad and it just sits idling not running the function, I've been messing with it for a couple hours but got nothing so far :/

I added this:

#include <GUIConstantsEx.au3>
;
Global $fRunOne = False
;
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
;
GUICreate("Test Script", 335, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Run this function when the [X] is clicked
;
GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
; No need to set a function for this control as it is not actioned
;
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
; No need to set a function for this control as it is not actioned
;
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
GUICtrlSetOnEvent($startbutton, "StartButtonPressed") ; Run this function when the button is pressed
;
$stopbutton = GUICtrlCreateButton("Stop", 190, 40, 60)
GUICtrlSetOnEvent($stopbutton, "StopButtonPressed") ; Run this function when the button is pressed
;
GUISetState(@SW_SHOW)
;
 While 1
     Sleep(1000)
     ; Check if the flag has been set by the OnEvent function
     If $fRunOne Then
         ; Now start the "real" function from within the main code
         StartButtonPressed()
     EndIf
 WEnd

; This function is run when the [X] is clicked
Func CLOSEClicked()
    MsgBox(0, "Stop", "GUI closed. Now Exiting")
    Exit
EndFunc   ;==>CLOSEClicked
;
; This function is run when the start button is pressed
Func StartButtonPressed()
$fRunOne = True
while 1
    $send1 = GUICtrlRead($key1)
    $sleep1 = GUICtrlRead($time1)
WEnd
MsgBox(0, "Read", "Key: " & $send1 & @CRLF & "Sleep: " & $sleep1)
Global $fRunOne = False

EndFunc   ;==>StartButtonPressed
;
; This function is run when the stop button is pressed
Func StopButtonPressed()
    MsgBox(0, "Stop", "Script Stopped. Now Exiting")
    Exit
EndFunc   ;==>StopButtonPressed

But the function won't exit the loop :/ I've been messing with exit loop and everything I can find in the Help file and coming up trumps ;)

Edited by cording12

Share this post


Link to post
Share on other sites
Melba23

cording12,

when you press start it doesn't actually press the keys

And where do you tell AutoIt to press keys? Take a look at Send in the Help file. ;)

the function won't exit the loop

Again you do not tell Autoit to exit the loop. When do you want to leave the loop? After a number of passes? If so, then look at the For...Next...Step page of the Help file. If not, when? :)

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

 

Share this post


Link to post
Share on other sites
cording12

It has to be on an infinite loop until told to be stopped. I shall take another look at the send dialogue! Thanks

Share this post


Link to post
Share on other sites
Melba23

cording12,

Interrupting a running function is not an easy task. ;)

Take a look at the Interrupting a running function tutorial in the Wiki to see how you can do it. Fortunately for you, you have chosen to use OnEvent mode, so that the problem is not too difficult to solve - look at the 3rd chunk of code in the tutorial to see how you might do it. :)

See how you get on doing it yourself and come back if you get stuck. :)

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

 

Share this post


Link to post
Share on other sites
cording12

Thanks for the reply. Think I was over tired when I did it last and having had a second look think I nearly finished it!!

Here's what I got now

#include <GUIConstantsEx.au3>
;
Global $fRunOne = False
;
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
;
GUICreate("Test Script", 335, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Run this function when the [X] is clicked
;
GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
; No need to set a function for this control as it is not actioned
;
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
; No need to set a function for this control as it is not actioned
;
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
GUICtrlSetOnEvent($startbutton, "StartButtonPressed") ; Run this function when the button is pressed
;
$stopbutton = GUICtrlCreateButton("Stop", 190, 40, 60)
GUICtrlSetOnEvent($stopbutton, "StopButtonPressed") ; Run this function when the button is pressed
;
GUISetState(@SW_SHOW)
;
While 1
     Sleep(1000)
     ; Check if the flag has been set by the OnEvent function
     If $fRunOne Then
         ; Now start the "real" function from within the main code
         StartButtonPressed_Run()
     EndIf
WEnd
;
Func StartButtonPressed ()
  $fRunOne = True
EndFunc ;==>StartButtonPressed
; This function is run when the start button is pressed
Func StartButtonPressed_Run()
While 1
  Send ($key1)
  Sleep ($time1)
WEnd
  Global $fRunOne = False
EndFunc   ;==>StartButtonPressed_Run
; This function is run when the [X] is clicked
Func CLOSEClicked()
    MsgBox(0, "Stop", "GUI closed. Now Exiting")
ToolTip ("Keys Stopped")
    Exit
EndFunc   ;==>CLOSEClicked
; This function is run when the stop button is pressed
Func StopButtonPressed()
    MsgBox(0, "Stop", "Script Stopped. Now Exiting")
Exit
EndFunc   ;==>StopButtonPressed

The only problem is I don't know how to use the Send function and tell it to read what keys have been input :/

Edit: OK I think its working but when tested it spams '64'... a lot

Edit 2: I was being stupid. I made it send $time instead of waiting that much time. I updated to code here but now all it will do is send the key "4" no matter what very quickly - just ignores what I input :/

Edited by cording12

Share this post


Link to post
Share on other sites
Melba23

cording12,

Pretty close! ;)

See if you can spot the differences: :)

#include <GUIConstantsEx.au3>
;
Global $fRunOne = False
;
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
;
GUICreate("Test Script", 335, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Run this function when the [X] is clicked
;
GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
; No need to set a function for this control as it is not actioned
;
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
; No need to set a function for this control as it is not actioned
;
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
GUICtrlSetOnEvent($startbutton, "StartButtonPressed") ; Run this function when the button is pressed
;
$stopbutton = GUICtrlCreateButton("Stop", 190, 40, 60)
GUICtrlSetOnEvent($stopbutton, "StopButtonPressed") ; Run this function when the button is pressed
;
GUISetState(@SW_SHOW)
;
While 1
    Sleep(10) ; <<<<<<<<<<<<<<<<<<<<<<<< You only need 10ms to keep the CPU cool and keep the loop active
    ; Check if the flag has been set by the OnEvent function
    If $fRunOne Then
        ; Now start the "real" function from within the main code
        StartButtonPressed_Run()
    EndIf
WEnd
;
Func StartButtonPressed()
    $fRunOne = True ; <<<<<<<<<<<<<<<<<<<<<<<<<<< Set the flag
EndFunc   ;==>StartButtonPressed
; This function is run when the start button is pressed
Func StartButtonPressed_Run()
    While $fRunOne ; <<<<<<<<<<<<<<<<<<<<<<<<<< You need to look for the flag here and check if the Stop button has been pressed and cleared the flag
        Send(GUICtrlRead($key1)) ; <<<<<<<<<<<<<<<<<<<<<<<<<< And here you need to read the control - you were sending the ControlID
        Sleep(GUICtrlRead($time1)) ; And here too
    WEnd
EndFunc   ;==>StartButtonPressed_Run
; This function is run when the [X] is clicked
Func CLOSEClicked()
    MsgBox(0, "Stop", "GUI closed. Now Exiting")
    ToolTip("Keys Stopped")
    Exit
EndFunc   ;==>CLOSEClicked
; This function is run when the stop button is pressed
Func StopButtonPressed()
    $fRunOne = False ; <<<<<<<<<<<<<<<<<< Set the flag to false here to stop the loop
EndFunc   ;==>StopButtonPressed

All clear? :)

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

 

Share this post


Link to post
Share on other sites
cording12

So simple now that you've shown me!! Thanks so much ;) Now I can just work on making it have a pretty GUI :3

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
Sign in to follow this  

×