Jump to content

GUICreate password window with Enter Key Problem


Go to solution Solved by Zedna,

Recommended Posts

Posted

Following code doesn't allowed enter key to be used for cancel.

I am trying to set up a GUI password login window where there are two passwords valid for this window. It works but my issue is the Enter key doesn't work for cancel. You have to click on cancel instead of tabbing to it and pressing enter. Is there a better way to do this. Note: I am using a GUICreate so I can handle when the screen is moved around by the user.

 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <ButtonConstants.au3>
#include <FontConstants.au3>
#include <StringConstants.au3>
#include <INet.au3>

;/ Global variables (in caps)
Global Const $TITLE = "MyTool v1"
Global Const $MENUFONT = "" ;i.e. "Comic Sans Ms"
Global Const $FONTSIZE = 16
Global Const $W = 400  ;/Width,Height,X,and Y (location of display windows)
Global Const $H = 400
Global $X = 192
Global $Y = 224
Global Const $NO_AUTO_LOGIN = "NO_AUTO_LOGIN"

;/menu color hex codes
Global Const $BLUE    = "0x63a3db"
Global Const $RED     = "0xff0000"
Global Const $YELLOW  = "0xFFC300"
Global Const $LTGREEN = "0x8AFF8A"
Global Const $BLACK   = "0x000000"

Global Const $InMenuDelay = 600

Example()

Func Example()

   $hGUI = GUICreate($TITLE, $W, $H, $X, $Y)
   GUISetBkColor($YELLOW)
   GUICtrlCreateLabel("Please Enter Password", 10, 10, 320)

   $tInput = GUICtrlCreateInput("", 10, 300, 380, 30, $ES_PASSWORD)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $bSelect1 = GUICtrlCreateButton("OK", 75, 340, 100, 30)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $bSelect2 = GUICtrlCreateButton("CANCEL", 225, 340, 100, 30)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $iENTER = GUICtrlCreateDummy()
   Dim $AccelKeys[1][2] = [["{ENTER}", $iENTER]]; Set accelerators
   GUISetAccelerators($AccelKeys)

   GUICtrlSetState($tInput, $GUI_FOCUS)
   GUISetState(@SW_SHOW)

   Local $FailedAttempts = 0
   Local $UserType = "USER"

   $nMsg = 0
   While $nMsg <> $GUI_EVENT_CLOSE
      ;SetXYPosition() ;/ adjust window X,Y location if user moves it
      $nMsg = GUIGetMsg()

      If $nMsg = $bSelect2 Then
         SplashTextOn($TITLE,@CRLF & @CRLF & "<< USER QUIT >>", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
         Sleep(600)
         SplashOff()
         Exit
      EndIf
      If $nMsg = $iENTER Or $nMsg = $bSelect1 Then
         If GUICtrlRead($tInput) = '' Then
            ContinueLoop
         EndIf
         If GUICtrlRead($tInput) = 'admin' Then
            $UserType = 'USER'
            ExitLoop
         ElseIf GUICtrlRead($tInput) = 'user' Then
            $UserTYpe = 'ADMIN'
            ExitLoop
         Else
            $FailedAttempts += 1
            SplashTextOn($TITLE, "Password Incorrect"  & @CRLF & @CRLF & "(" & $FailedAttempts &") failed attempts.", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
            Sleep(1000)
            SplashOff()
            If $FailedAttempts >= 3 Then
               SplashTextOn($TITLE, "Too Many attempts made," & @CRLF & @CRLF & "Logged and Reported to Support", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
               Sleep(1000)
               SplashOff()
               $SystemEvent='EVENTCREATE /T ERROR /ID 99 /L APPLICATION /SO SomeTOOL /D "Error: User Exceeded Max Allowed Login Attempts"'
               Run(@ComSpec & " /c " & $SystemEvent, "", @SW_HIDE)
               Sleep(200)
               Exit
            EndIf
         EndIf
         GUICtrlSetData($tInput, "")
         GUICtrlSetState($tInput, $GUI_FOCUS)
      EndIf
   WEnd

 

  • Solution
Posted

Use Func _IsFocused($hWnd, $iControl)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <ButtonConstants.au3>
#include <FontConstants.au3>
#include <StringConstants.au3>
#include <INet.au3>

;/ Global variables (in caps)
Global Const $TITLE = "MyTool v1"
Global Const $MENUFONT = "" ;i.e. "Comic Sans Ms"
Global Const $FONTSIZE = 16
Global Const $W = 400  ;/Width,Height,X,and Y (location of display windows)
Global Const $H = 400
Global $X = 192
Global $Y = 224
Global Const $NO_AUTO_LOGIN = "NO_AUTO_LOGIN"

;/menu color hex codes
Global Const $BLUE    = "0x63a3db"
Global Const $RED     = "0xff0000"
Global Const $YELLOW  = "0xFFC300"
Global Const $LTGREEN = "0x8AFF8A"
Global Const $BLACK   = "0x000000"

Global Const $InMenuDelay = 600

   $hGUI = GUICreate($TITLE, $W, $H, $X, $Y)
   GUISetBkColor($YELLOW)
   GUICtrlCreateLabel("Please Enter Password", 10, 10, 320)

   $tInput = GUICtrlCreateInput("", 10, 300, 380, 30, $ES_PASSWORD)
   $hInput = GUICtrlGetHandle($tInput)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $bSelect1 = GUICtrlCreateButton("OK", 75, 340, 100, 30)
   $hSelect1 = GUICtrlGetHandle($bSelect1)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $bSelect2 = GUICtrlCreateButton("CANCEL", 225, 340, 100, 30)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $iENTER = GUICtrlCreateDummy()
   Dim $AccelKeys[1][2] = [["{ENTER}", $iENTER]]; Set accelerators
   GUISetAccelerators($AccelKeys)

   GUICtrlSetState($tInput, $GUI_FOCUS)
   GUISetState(@SW_SHOW)

   Local $FailedAttempts = 0
   Local $UserType = "USER"

   $nMsg = 0
   While $nMsg <> $GUI_EVENT_CLOSE
      ;SetXYPosition() ;/ adjust window X,Y location if user moves it
      $nMsg = GUIGetMsg()

      If $nMsg = $bSelect2 Then
         SplashTextOn($TITLE,@CRLF & @CRLF & "<< USER QUIT >>", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
         Sleep(600)
         SplashOff()
         Exit
      EndIf
;~       If $nMsg = $iENTER Or $nMsg = $bSelect1 Then
      If ($nMsg = $iENTER And (_IsFocused($hGUI, $hInput) Or _IsFocused($hGUI, $hSelect1))) Or ($nMsg = $bSelect1) Then

         If GUICtrlRead($tInput) = '' Then
            ContinueLoop
         EndIf
;~          If GUICtrlRead($tInput) = 'admin' Then
;~             $UserType = 'USER'
;~             ExitLoop
;~          ElseIf GUICtrlRead($tInput) = 'user' Then
;~             $UserTYpe = 'ADMIN'
;~             ExitLoop
;~          Else
;~             $FailedAttempts += 1
;~             SplashTextOn($TITLE, "Password Incorrect"  & @CRLF & @CRLF & "(" & $FailedAttempts &") failed attempts.", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
;~             Sleep(1000)
;~             SplashOff()
;~             If $FailedAttempts >= 3 Then
;~                SplashTextOn($TITLE, "Too Many attempts made," & @CRLF & @CRLF & "Logged and Reported to Support", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
;~                Sleep(1000)
;~                SplashOff()
;~                $SystemEvent='EVENTCREATE /T ERROR /ID 99 /L APPLICATION /SO SomeTOOL /D "Error: User Exceeded Max Allowed Login Attempts"'
;~                Run(@ComSpec & " /c " & $SystemEvent, "", @SW_HIDE)
;~                Sleep(200)
;~                Exit
;~             EndIf
;~          EndIf
         MsgBox(0,'',GUICtrlRead($tInput))
         GUICtrlSetData($tInput, "")
         GUICtrlSetState($tInput, $GUI_FOCUS)
      EndIf
  WEnd

Func _IsFocused($hWnd, $iControl)
    Return ControlGetHandle($hWnd, "", ControlGetFocus($hWnd)) = $iControl
EndFunc   ;==>_IsFocused

 

Posted

As an update , I added an hSeclect2 handle and changed the check a little bit to allow press button or enter key to close the box, your input was invaluable. 

 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <ButtonConstants.au3>
#include <FontConstants.au3>
#include <StringConstants.au3>
#include <INet.au3>

;/ Global variables (in caps)
Global Const $TITLE = "MyTool v1"
Global Const $MENUFONT = "" ;i.e. "Comic Sans Ms"
Global Const $FONTSIZE = 16
Global Const $W = 400  ;/Width,Height,X,and Y (location of display windows)
Global Const $H = 400
Global $X = 192
Global $Y = 224
Global Const $NO_AUTO_LOGIN = "NO_AUTO_LOGIN"

;/menu color hex codes
Global Const $BLUE    = "0x63a3db"
Global Const $RED     = "0xff0000"
Global Const $YELLOW  = "0xFFC300"
Global Const $LTGREEN = "0x8AFF8A"
Global Const $BLACK   = "0x000000"

Global Const $InMenuDelay = 600

   $hGUI = GUICreate($TITLE, $W, $H, $X, $Y)
   GUISetBkColor($YELLOW)
   GUICtrlCreateLabel("Please Enter Password", 10, 10, 320)

   $tInput = GUICtrlCreateInput("", 10, 300, 380, 30, $ES_PASSWORD)
   $hInput = GUICtrlGetHandle($tInput)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $bSelect1 = GUICtrlCreateButton("OK", 75, 340, 100, 30)
   $hSelect1 = GUICtrlGetHandle($bSelect1)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $bSelect2 = GUICtrlCreateButton("CANCEL", 225, 340, 100, 30)
   $hSelect2 = GUICtrlGetHandle($bSelect2)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, "", $MENUFONT)

   $iENTER = GUICtrlCreateDummy()
   Dim $AccelKeys[1][2] = [["{ENTER}", $iENTER]]; Set accelerators
   GUISetAccelerators($AccelKeys)

   GUICtrlSetState($tInput, $GUI_FOCUS)
   GUISetState(@SW_SHOW)

   Local $FailedAttempts = 0
   Local $UserType = "USER"

   $nMsg = 0
   While $nMsg <> $GUI_EVENT_CLOSE
      ;SetXYPosition() ;/ adjust window X,Y location if user moves it
      $nMsg = GUIGetMsg()

      If ($nMsg = $iENTER And (_IsFocused($hGUI, $hSelect2))) Or ($nMsg = $bSelect2) Then
         SplashTextOn($TITLE,@CRLF & @CRLF & "<< USER QUIT >>", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
         Sleep(600)
         SplashOff()
         Exit
      EndIf
;~       If $nMsg = $iENTER Or $nMsg = $bSelect1 Then
      If ($nMsg = $iENTER And (_IsFocused($hGUI, $hInput) Or _IsFocused($hGUI, $hSelect1))) Or ($nMsg = $bSelect1) Then

         If GUICtrlRead($tInput) = '' Then
            ContinueLoop
         EndIf
;~          If GUICtrlRead($tInput) = 'admin' Then
;~             $UserType = 'USER'
;~             ExitLoop
;~          ElseIf GUICtrlRead($tInput) = 'user' Then
;~             $UserTYpe = 'ADMIN'
;~             ExitLoop
;~          Else
;~             $FailedAttempts += 1
;~             SplashTextOn($TITLE, "Password Incorrect"  & @CRLF & @CRLF & "(" & $FailedAttempts &") failed attempts.", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
;~             Sleep(1000)
;~             SplashOff()
;~             If $FailedAttempts >= 3 Then
;~                SplashTextOn($TITLE, "Too Many attempts made," & @CRLF & @CRLF & "Logged and Reported to Support", $W, $H, $X, $Y, $DLG_CENTERONTOP, "", $FONTSIZE)
;~                Sleep(1000)
;~                SplashOff()
;~                $SystemEvent='EVENTCREATE /T ERROR /ID 99 /L APPLICATION /SO SomeTOOL /D "Error: User Exceeded Max Allowed Login Attempts"'
;~                Run(@ComSpec & " /c " & $SystemEvent, "", @SW_HIDE)
;~                Sleep(200)
;~                Exit
;~             EndIf
;~          EndIf
         MsgBox(0,'',GUICtrlRead($tInput))
         GUICtrlSetData($tInput, "")
         GUICtrlSetState($tInput, $GUI_FOCUS)
      EndIf
  WEnd

Func _IsFocused($hWnd, $iControl)
    Return ControlGetHandle($hWnd, "", ControlGetFocus($hWnd)) = $iControl
EndFunc   ;==>_IsFocused

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...