Sign in to follow this  
Followers 0
PhilHibbs

Exiting a script with {Esc}

6 posts in this topic

I think I need to re-think the way I get my scripts to terminate. Currently I just use HotKeySet("{Esc}","ExitScript"), but this doesn't work if I'm running more than one script at a time. Perhaps I should use _IsPressed in my While True loop. Any other suggestions?

Share this post


Link to post
Share on other sites



PhilHibbs,

You can look for various things in the GUIGetMsg loop; $GUI_EVENT_CLOSE (produced by the red [X] of the GUI itself) or the ControlIDs of a suitable control or menu item you have created. If you want to use a tray item, you need to use TrayGetMsg - or use TrayItemSetOnEvent.

An example:

#include <GUIConstantsEx.au3>

; Create tray menu
Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")

TraySetState()

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$mFileMenu = GUICtrlCreateMenu("&File")
$mExitItem = GUICtrlCreateMenuItem("&Exit", $mFileMenu)

$hExitButton = GUICtrlCreateButton("Exit", 10, 10, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $mExitItem, $hExitButton
            Exit
    EndSwitch

WEnd

Func On_Exit()
    Exit
EndFunc

There is also Opt(GUICloseOnESC) which basically sends $GUI_EVENT_CLOSE when you press Escape - yet another exit point unless you disable it.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

You can look for various things in the GUIGetMsg loop;

I don't see how any of that helps me to make my script exit when I press {Esc} regardless of what window is active. One of us is misunderstanding the other.

Share this post


Link to post
Share on other sites

PhilHibbs,

One of us is misunderstanding the other

I quite agree. :)

Perhaps Accelerators might help you. Run this script twice from SciTE, or better compile it, copy it and rename the copy. You will find that pressing "Esc" only exits the active script.

#include <GUIConstantsEx.au3>

Opt("GUICloseOnESC", 0) ; prevent normal ESC = Exit

$hGUI = GUICreate(@ScriptName, 500, 500)

$hDummy = GUICtrlCreateDummy()

GUISetState()
Dim $AccelKeys[1][2]=[["{ESC}", $hDummy]]
GUISetAccelerators($AccelKeys)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDummy
            ConsoleWrite("Hit" & @CRLF)
            Exit
    EndSwitch

WEnd

I hope we are on the same page this time. :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

I hope we are on the same page this time. :(

Not quite. The scripts I'm talking about don't have a GUI. They run in the background and interact with another application window. If I'm running more than one of them, I want them all to exit when I press {Esc}.

Share this post


Link to post
Share on other sites

PhilHibbs,

It might have helped to give a bit more information in the first post. :(

My last suggestion: Run a mother script which looks over its offspring and then terminates them all if one is closed. If you were to use this script as the launcher for the others (as you say you need a variable number running at any one time) it would be really simple to collect the PIDs and so keep an eye on them.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

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