2
\$\begingroup\$

I have an input field password box, currently the password is 'submitted' either by clicking Enter or by clicking elsewhere on the screen. This seems to be the designed way, as it states this in the documentation (https://docs.unity3d.com/Manual/script-InputField.html) -

"A UnityEvent that is invoked when the user finishes editing the text content either by submitting or by clicking somewhere that removes the focus from the Input Field."

As I have other buttons on the same screen that users can click on, I would like it so mouse clicks are not counted as an invoke.

Is this possible?

Many thanks in advance

\$\endgroup\$
2
  • \$\begingroup\$ Have you tried using an Input event to check when the key pressed, and then manually checking the input field? Something such as: if(Input.GetKeyDown(KeyCode.Enter)){ }? \$\endgroup\$
    – user84154
    Jan 17 '17 at 16:22
  • \$\begingroup\$ That part of the input field is not really for submitting the data. It's for checking whether the data is in the form you want. You shouldn't use that part and instead write the code @GMR516 suggested in another script and do the password checking process there. \$\endgroup\$ Jan 18 '17 at 8:06
1
\$\begingroup\$

You could use this method to check if the password is correct every time a new character is entered into the text field

https://docs.unity3d.com/ScriptReference/UI.InputField-onValueChange.html

You may want to add some checks before seeing if it is correct, like only checking once password is over minimum length so that you don't send unnecessary requests to the login server.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Hi, thanks for the response. I don't want the password to invoke as soon as the password has been typed correctly, what I would like is for it to only do so when ENTER is pressed. The reason being that I have a 'Show Password' toggle button, if the user selects this toggle while typing the password, it counts it as an incorrect password attempt (if it is incomplete) which looks messy. \$\endgroup\$
    – NIMBLE JIM
    Jan 17 '17 at 14:19
  • \$\begingroup\$ Just check for when Enter is input as a value? \$\endgroup\$ Jan 18 '17 at 9:31
0
\$\begingroup\$

Simple solution:

using UnityEngine;
using UnityEngine.UI;

public class CustomSubmit : MonoBehaviour
{
    public Button SomeButton;
    public InputField Field;
    private bool wasFocused;

    private void Awake()
    {
        SomeButton.onClick.AddListener(() => {
            Submit(Field.text);
        });
    }

    private void Update()
    {
        if (wasFocused && Input.GetKeyDown(KeyCode.Return)) {
            Submit(Field.text);
        }

        wasFocused = Field.isFocused;
    }

    private void Submit(string text)
    {
        Debug.Log("Submit=" + text);
    }
}
\$\endgroup\$
9
  • \$\begingroup\$ "As I have other buttons on the same screen that users can click on, I would like it so mouse clicks are not counted as an invoke." - please describe in more detail, because I faced the same onEndEdit issue, and this simple solution works well with other buttons. \$\endgroup\$
    – gresolio
    May 28 '17 at 17:47
  • \$\begingroup\$ It's a pity that onEndEdit designed in such a strange way, and there is no option to change its behaviour. \$\endgroup\$
    – gresolio
    May 28 '17 at 18:03
  • \$\begingroup\$ It's not an issue at all :) SomeButton.onClick = new Button.ButtonClickedEvent(); SomeButton.onClick.AddListener(() => { Submit(Field.text); }); \$\endgroup\$
    – gresolio
    May 28 '17 at 23:37
  • 1
    \$\begingroup\$ It was just a proof of concept, how one can write custom "Submit", that react to ENTER only and not a click/loss of focus. Other buttons can be simply added to this scenario if required. \$\endgroup\$
    – gresolio
    May 28 '17 at 23:47
  • \$\begingroup\$ I edited the answer to illustrate what I mean (added a button example). \$\endgroup\$
    – gresolio
    May 28 '17 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.