Search Unity

TextMeshPro onSubmit() being called with escape key

Discussion in 'UGUI & TextMesh Pro' started by therobby3, Mar 13, 2019.

  1. therobby3

    therobby3

    Joined:
    Jan 30, 2019
    Posts:
    131
    Simple problem, I wondering if it's a bug or if I'm somehow doing something wrong. I'm using the onSubmit event dispatched from a Text Mesh Pro input field. It works whenever a user pressed enter... however it also works if a user presses escape. So if a player would type something wrong and decide they want to leave the input field by pressing escape, their message would get sent. Does anyone else have this problem?

    The sample code below is all that is needed.

    Code (CSharp):
    1. TMP_InputField Input_txt;
    2. Input_txt.onSubmit.AddListener(sayChatMessage);
    3.  
    4. void sayChatMessage(string msg){
    5.      print("called!"); //Fires if a user pressed enter, and also fired if they press escape.
    6. }
     
    cxode likes this.
  2. BorjaFAC

    BorjaFAC

    Joined:
    Sep 29, 2015
    Posts:
    9
    Still happening in 2019.2. Any clue?
     
  3. zakkaiokenx10

    zakkaiokenx10

    Joined:
    Mar 26, 2018
    Posts:
    4
    onsubmit is not calling at all for me I'm super confused.
     
  4. HackerOfDreams

    HackerOfDreams

    Joined:
    Oct 30, 2016
    Posts:
    1
    You can check on the event listener method for the KeyCode.Escape and return before doing anything. In you example would be something like:

    Code (CSharp):
    1. TMP_InputField Input_txt;
    2. Input_txt.onSubmit.AddListener(sayChatMessage);
    3.  
    4. void sayChatMessage(string msg){
    5.      if (Input.GetKeyDown(KeyCode.Escape)) { return; }
    6.      print("called!"); //Fires only on Enter key.
    7. }
     
  5. santiandrade

    santiandrade

    Joined:
    Sep 2, 2013
    Posts:
    13
    There is exists a more elegant way to solve it and non-dependent on the Input class. You can use the "wasCanceled" property included in the own TMP_InputField in order to know if the ESC key was pressed or not.

    Somethink like this:

    Code (CSharp):
    1. TMP_InputField Input_txt;
    2. Input_txt.onSubmit.AddListener(sayChatMessage);
    3.  
    4. void sayChatMessage(string msg)
    5. {
    6.      if (Input_txt.wasCanceled)
    7.           return;
    8.  
    9.      print("called!"); //Fires only on Enter key.
    10. }
     
  6. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    I recommend using an extension method for this:

    Code (CSharp):
    1. public static class TMP_InputFieldExtensions
    2. {
    3.     /// <summary>
    4.     /// TextMeshPro does this brilliant thing where it triggers the <see cref="TMP_InputField.onSubmit"/> event both when you press
    5.     /// enter to submit, and also when you press escape to cancel. If you subscribe to the event using this extension method instead,
    6.     /// your callback will only be fired when the input field is ACTUALLY submitted (i.e. the user presses enter and not escape).
    7.     /// </summary>
    8.     public static void OnActuallySubmitted(this TMP_InputField inputField, Action<string> onActuallySubmittedCallback)
    9.     {
    10.         inputField.onSubmit.AddListener(text =>
    11.         {
    12.             if (!inputField.wasCanceled)
    13.                 onActuallySubmittedCallback?.Invoke(text);
    14.         });
    15.     }
    16. }
    Usage:

    Code (CSharp):
    1. inputField.onSubmit.AddListener(SubmittedCallback);
    2.  
    3. // Becomes...
    4.  
    5. inputField.OnActuallySubmitted(SubmittedCallback);
     
  7. spajus

    spajus

    Joined:
    Jun 10, 2015
    Posts:
    47
  8. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    That doesn't work if you actually do want the original text to be restored on escape