Gui Capture of text in fields Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Gui Capture of text in fields

Post by roysubs » 11 Apr 2021, 02:06

My username / password autofill project is going really well and now saves me a huge amount of time logging into lots of different servers in various dialogues and RDP sessions and managing passwords, but I've hit a snag in updating values in my password file. My "Change credentials" Gui reads from a file to populate a list, from which I pick a value to change and that works, the Gui here is populated with the current values correctly, but when I click on "OK", any new values that I have typed in do not populate the variable, so I'm missing something on how this works. Basically, I enter a new value in a field and I press OK. When I hit the "OK" button (or press Enter), how do I get the newly typed value in the fields to populate the Gui variables, there must be some command to do this I'm thinking, as the Gui variables are simply not updated when I press OK (although I've tried some ways, as below).

This is the last part of what I need for the project, once this is working I'll post it up somewhere on the site and hopefully some guru's can suggest improvements to make it even better (but already it helps me a huge amount, very useful/efficient time-saving in user/password entry with the other components).

Code: Select all

:x*:]]change::
    usernames := ""
    GuiTag := "", GuiUsername := "", GuiPassword := "", GuiEmail := "", GuiFullname := ""

    Loop, read, %PwdFile%
    {
        if (!RegExMatch(A_LoopReadLine, "^#") {
            tmp := StrSplit(A_LoopReadLine, " ")
            usernames .= tmp.2  . "     [" . tmp.1 . "]`n"
        }
    }
	
	; This simply uses the "SelectItem" function to display a RadioBox Gui to pick from the above list of usernames to then capture a line from the %PwdFile% to work with
    selection := SelectItem(usernames, "Select User/Tag to be changed?", "Up/Down then Enter to select User/Tag to change.`nThe code in [xx] account tag to tie to password.")

    if (selection != "") {
        tmp := StrSplit(RegExReplace(selection, " +", " "), " ")   ; use regex to replace multiple spaces with single spaces!
        tmp_last := tmp.MaxIndex()
        tag := tmp[tmp_last]
        tag := RegExReplace(tag, "\[", "")
        tag := RegExReplace(tag, "\]", "")
    }

    ; GetPwdValue simply looks into the %PwdFile% and grabs a line that starts with %tag% (each line is simply a space-separated string of "<tag> <username> <password> <email> <fullname>" that GetPwdValue splits up)
    user  := GetPwdValue(tag, "Username")
    pass  := GetPwdValue(tag, "Password")
    email := GetPwdValue(tag, "Email")
    full  := GetPwdValue(tag, "Fullname")

    GuiTag := tag   ; don't think I need to do this, just trying to pre-populate them to start with to see if this helps
    GuiUsername := user
    GuiPassword := pass
    GuiEmail := email
    GuiFullname := full

    gui,3: new
    gui,3: Default
    gui,3: +LastFound ; +Label2
    gui,3: add, text, xm12 ym5,Enter credentials to store in password file at:
    gui,3: add, text, xm12 ym20,'%PwdFile%'.
    gui,3: add, text, xm12 ym45,Tag, Username, Password are mandatory.   ; groupbox w250 h147
    gui,3: add, text, xm12 ym60,Email and Fullname are optional.   ; groupbox w250 h147
    gui,3: add, edit, xm80 ys ym85 vGuiTag, %tag%
    gui,3: add, edit, xm80 yp+30 vGuiUsername, %user%
    gui,3: add, edit, xm80 yp+30 vGuiPassword, %pass%   ; 'password' option will obscure this field
    gui,3: add, edit, xm80 yp+30 vGuiEmail, %email%
    gui,3: add, edit, xm80 yp+30 vGuiFullname, %full%
    gui,3: add, text, xm12 ym87 section, Tag:
    gui,3: add, text, xm12 yp+30, Username:
    gui,3: add, text, xm12 yp+30, Password:
    gui,3: add, text, xm12 yp+30, (Email):
    gui,3: add, text, xm12 yp+30, (Fullname):
    gui,3: add, button, xm80 yp+35 default gDone3, OK      ; Default means this will act on pressing Enter
    gui,3: add, button, xp+50 gClose, Cancel
    gui,3: show, x300 y200, Set Credentials
    return     ; return should be blank except inside a function... winexist()
    
    Done3:
    {
		I want to populate the current values in the Gui fields as they are when I press OK. But this doesn't work, when I get to this point, none of GuiTag / Username / Password etc seem to contain the CURRENT values that are in those fields!
        tag := GuiTag
        user := GuiUsername
        pass := GuiPassword
        email := GuiEmail
        full := GuiFullname

        MsgBox, %tag%`n%user%`n%pass%
        gui,2:submit,nohide

        if (GuiTag = tag and GuiUsername = user and GuiPassword = pass and GuiEmail = email and GuiFullname = full) {
            MsgBox, No values were changed., No changes were made, exiting...
            gui,2:destroy
            gui,3:destroy
            return
        }
        
        IfInString, GuiTag, %A_Space%
        {
            MsgBox, Tag '%GuiTag%' is not permitted to have whitespace
            gui,2:destroy
            return
        }
        IfInString, GuiUsername, %A_Space%
        {
            MsgBox, Username '%GuiUsername%' is not permitted to have whitespace
            gui,2:destroy
            return
        }
        IfInString, GuiPassword, %A_Space%
        {
            MsgBox, Password '%GuiPassword%' is not permitted to have whitespace
            gui,2:destroy
            return
        }
        
        if (GuiTag != "" and GuiUsername != "" and GuiPassword != "") {
            SetPWdTag(GuiTag, GuiUsername, GuiPassword, GuiEmail, GuiFullname)
        }
        else {
            MsgBox, Mandatory values are required for Tag, Username, Password. As these were not entered, nothing will be updated in '%PwdFile%':`n`nTag:   %GuiTag%`nUsername:   %GuiUsername%`nPassword:   %GuiPassword%`nEmail:   %GuiEmail%`nFullname:   %GuiFullname%
        }
        gui,2:destroy
        gui,3:destroy
        return
    }
return

; Must match the gui title (and cannot use variales)
#IfWinActive Set Credentials   ; Without this, ESC will be globally tied to this dialogue, locking out all other functionality
Esc::
Close:
{
    gui,2:destroy
    gui,3:destroy
    return
}
#IfWinActive
User avatar
mikeyww
Posts: 26890
Joined: 09 Sep 2014, 18:38

Re: Gui Capture of text in fields

Post by mikeyww » 11 Apr 2021, 05:45

This script obviously cannot be tested here-- and also contains a syntax error-- but in general, one would submit GUI 3, or any GUI, to find its values.
Last edited by mikeyww on 11 Apr 2021, 05:47, edited 2 times in total.
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Gui Capture of text in fields

Post by roysubs » 11 Apr 2021, 05:46

How do you mean "Submit GUI 3"? I think this sounds like all I'm needing here, but how is that done?
User avatar
mikeyww
Posts: 26890
Joined: 09 Sep 2014, 18:38

Re: Gui Capture of text in fields  Topic is solved

Post by mikeyww » 11 Apr 2021, 05:48

Code: Select all

Gui, 3:New
Gui, Font, s10
Gui, Add, Edit, vttext
Gui, Add, Button, Default, OK
Gui, Show, w200, Test
Return

3ButtonOK:
Gui, 3:Submit
MsgBox, %ttext%
Return
Instead of building your GUI and destroying it each time, you can build it once at the start of your script, and then just Submit and Show it when needed.

Code: Select all

Gui, 3:New
Gui, Font, s10
Gui, Add, Edit, vttext
Gui, Add, Button, Default, OK

F3::Gui, 3:Show, w200, Test

3ButtonOK:
Gui, 3:Submit
MsgBox, %ttext%
GuiControl,, ttext
Return
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Gui Capture of text in fields

Post by roysubs » 13 Apr 2021, 00:28

Great, thanks Mikey. Ok, the first part fixed my dialogue and then it correctly updates my password file as I expected (fantastic!). :)

I like this thing about declaring the Gui at start of script, but it has specific variables that have to be populated upon invocation, so I'll have to play around with that to see what works, thanks for that tip, very useful.
User avatar
mikeyww
Posts: 26890
Joined: 09 Sep 2014, 18:38

Re: Gui Capture of text in fields

Post by mikeyww » 13 Apr 2021, 05:27

OK. You can also use GuiControl to change the values of the controls (e.g., an edit field).
Post Reply

Return to “Ask for Help (v1)”