Jump to content

Pressing the enter key exits my script, why?


Recommended Posts

Hope someone can help here:

My script puts a GUI up with two buttons, "Exit" and "Install"

I don't have any code using the _IsPressed command, yet whilst the GUI is up if I press the Enter key on my keyboard the script exits.

I'm assuming this is because the Exit button (which does exit the script) is selected by default somehow. I tried changing this to the other button but I still can't get it to work. Ideas?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

; Create UI with input boxes to capture user details
$Form1 = GUICreate("Installation utility", 429, 218, 368, 197, BitOR($WS_SYSMENU,$WS_CAPTION,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS), BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$EmailInput = GUICtrlCreateInput ("", 184, 23, 217, 21)
$pw1 = GUICtrlCreateInput("", 184, 71, 217, 21)
$pw2 = GUICtrlCreateInput("", 184, 118, 217, 21)
$InstallButton = GUICtrlCreateButton("Install", 240, 176, 75, 25, BitOR($BS_DEFPUSHBUTTON,$WS_GROUP))
$ExitButton = GUICtrlCreateButton("Exit", 104, 176, 75, 25, BitOR($BS_DEFPUSHBUTTON,$WS_GROUP))
$Label1 = GUICtrlCreateLabel("Enter the users email address:", 32, 24, 145, 17)
$Label2 = GUICtrlCreateLabel("Enter a password for this account:", 16, 72, 165, 17)
$Label3 = GUICtrlCreateLabel("(min 8 characters)", 48, 88, 88, 17)
$Label4 = GUICtrlCreateLabel("(e.g. user@company.com)", 40, 40, 128, 17)
$Label5 = GUICtrlCreateLabel("Confirm password:", 88, 120, 90, 17)
GUISetState(@SW_SHOW)
$completed = 0

While 1
    $msg = GUIGetMsg()
       Select
        ; Exit if the user closes the dialogue box or clicks on Exit button
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $ExitButton
            Exit

        ; Exit if the installation process completed
        Case $completed = 1
            Exit

        ; Validate that the two passwords entered match
    Case $msg = $InstallButton
    If (GUICtrlRead($pw1)) == (GUICtrlRead($pw2)) and (GUICtrlRead($EmailInput)) <> "" then
                    MsgBox(262144, "Installing...", "Using this email address: " & GUICtrlRead($EmailInput) & @CRLF & " and this password: " & GUICtrlRead($pw1))
                    ;need to write command line part still

                    $completed = 1


                ElseIF (GUICtrlRead($pw1)) <> (GUICtrlRead($pw2)) then
                    MsgBox(262144+48, "Password error", "Passwords do not match, please try again.")
                    $completed = 0

                    ElseIf (GUICtrlRead($EmailInput)) == "" then
                    MsgBox(262144+48, "Email address error", "Please enter an email address and try again.")
                    $completed = 0


                EndIf
        EndSelect
WEnd
Link to comment
Share on other sites

Remove the '$BS_DEFPUSHBUTTON' style from your exit button. You can remove it from the install button as well.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

; Create UI with input boxes to capture user details
$Form1 = GUICreate("Installation utility", 429, 218, 368, 197, BitOR($WS_SYSMENU,$WS_CAPTION,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS), BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$EmailInput = GUICtrlCreateInput ("", 184, 23, 217, 21)
$pw1 = GUICtrlCreateInput("", 184, 71, 217, 21)
$pw2 = GUICtrlCreateInput("", 184, 118, 217, 21)
$InstallButton = GUICtrlCreateButton("Install", 240, 176, 75, 25, $WS_GROUP)
$ExitButton = GUICtrlCreateButton("Exit", 104, 176, 75, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("Enter the users email address:", 32, 24, 145, 17)
$Label2 = GUICtrlCreateLabel("Enter a password for this account:", 16, 72, 165, 17)
$Label3 = GUICtrlCreateLabel("(min 8 characters)", 48, 88, 88, 17)
$Label4 = GUICtrlCreateLabel("(e.g. user@company.com)", 40, 40, 128, 17)
$Label5 = GUICtrlCreateLabel("Confirm password:", 88, 120, 90, 17)
GUISetState(@SW_SHOW)
$completed = 0

While 1
    $msg = GUIGetMsg()
       Select
        ; Exit if the user closes the dialogue box or clicks on Exit button
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $ExitButton
            Exit

        ; Exit if the installation process completed
        Case $completed = 1
            Exit

        ; Validate that the two passwords entered match
    Case $msg = $InstallButton
    If (GUICtrlRead($pw1)) == (GUICtrlRead($pw2)) and (GUICtrlRead($EmailInput)) <> "" then
                    MsgBox(262144, "Installing...", "Using this email address: " & GUICtrlRead($EmailInput) & @CRLF & " and this password: " & GUICtrlRead($pw1))
                    ;need to write command line part still

                    $completed = 1


                ElseIF (GUICtrlRead($pw1)) <> (GUICtrlRead($pw2)) then
                    MsgBox(262144+48, "Password error", "Passwords do not match, please try again.")
                    $completed = 0

                    ElseIf (GUICtrlRead($EmailInput)) == "" then
                    MsgBox(262144+48, "Email address error", "Please enter an email address and try again.")
                    $completed = 0


                EndIf
        EndSelect
WEnd
Link to comment
Share on other sites

  • Moderators

saldous,

Both your buttons have the $BS_DEFPUSHBUTTON style. From the Help file for GUICtrlCreateButton:

$BS_DEFPUSHBUTTON - Creates a push button with a heavy black border. If the button is in a dialog box, the user can select the button by pressing the ENTER key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely option, or default.

You should only ever have one button with this style, but as the Exit button is given this style last, it will react when the Enter key is pressed.

Simple solution - remove the $BS_DEFPUSHBUTTON style from the button create code.

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