Search Unity

Keep mobile keyboard open

Discussion in 'UGUI & TextMesh Pro' started by icebear007, Jun 8, 2016.

  1. icebear007

    icebear007

    Joined:
    Jun 8, 2016
    Posts:
    67
    Hi,

    Is there a way to keep the mobile keyboard open after the user submitted the text? At the moment, Unity automatically closes the keyboard if a user touches outside of the keyboard area or if the user submits the text (click "done").
    As an easy example, after sending a message in WhatsApp, the keyboard remains open so the user can type in the next message directly.

    Thanks,
    Alex
     
    Deleted User likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I would have a look at the InputField ui script. Unity provides these online and you can google for them. I'm assuming you are using an input field? The important thing with the keyboard is

    Code (CSharp):
    1. public void DeactivateInputField()
    2.         {
    3.             // Not activated do nothing.
    4.             if (!m_AllowInput)
    5.                 return;
    6.  
    7.             m_HasDoneFocusTransition = false;
    8.             m_AllowInput = false;
    9.  
    10.             if (m_TextComponent != null && IsInteractable())
    11.             {
    12.                 if (m_WasCanceled)
    13.                     text = m_OriginalText;
    14.  
    15.                 if (m_Keyboard != null)
    16.                 {
    17.                     m_Keyboard.active = false;
    18.                     m_Keyboard = null;
    19.                 }
    20.  
    21.                 m_CaretPosition = m_CaretSelectPosition = 0;
    22.  
    23.                 SendOnSubmit();
    24.  
    25.                 Input.imeCompositionMode = IMECompositionMode.Auto;
    26.             }
    27.  
    28.             MarkGeometryAsDirty();
    29.         }
    If you notice there is an m_Keyboard.active = false. This will turn off the keyboard. So you may just be able to block that out. Or, you can always call m_keyboard.active = true if you want to display the keyboard.
     
  3. icebear007

    icebear007

    Joined:
    Jun 8, 2016
    Posts:
    67
    Hi Brathnann,

    Thanks for your answer. So far, I wasn't using an input field. I simply used

    Code (CSharp):
    1.  
    2. void OpenKeyboard()
    3. {
    4.     keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, true, false, false, false);
    5. }
    6. // ...
    7. void Update()
    8. {
    9.         if (keyboard != null && keyboard.done)
    10.         {
    11.                //  check keyboard ...
    12.         }
    13.         // ...
    14. }
    15.        
    16.  
    to create a keyboard and check it. I'll have a look at the input field methods.

    Alex
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    ah, I see. I assumed you had an input field where they were typing something into. So I was just suggesting you could modify the input field script to not close the keyboard when the input box is no longer focused.
     
  5. icebear007

    icebear007

    Joined:
    Jun 8, 2016
    Posts:
    67
    I played a bit with the inputfield script but I couldn't stop the keyboard from closing. I think the reason for that is the keyboard contains the logic for closing and therefore adding my own logic to the inputfield script is too late.

    Alex
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I'm at work, but I'll glance at the inputfield code when I get home much later today.
     
  7. Mike01923

    Mike01923

    Joined:
    Jun 19, 2015
    Posts:
    194
    Did you figure out a solution to this?
     
  8. BrokenAngel

    BrokenAngel

    Joined:
    Mar 24, 2013
    Posts:
    92
    Hi there , is there any solutions for this ?
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Soft / Virtual Keyboard behavior varies per platform. For instance on iOS, the keyboard remains visible even when focus is lost whereas on Android it does not. As such, you might be better served by implementing your own soft / virtual keyboard.
     
  10. habitoti

    habitoti

    Joined:
    Feb 28, 2013
    Posts:
    141
    Creating own virtual keyboard is really not feasible if you need to support global audience. The best keyboard for a user that he is used to is his own mobile keyboard, supporting his locale. My problem is that I do an online search in Firebase that updates per typed character and would allow you also to scroll through the current result list. However on Android, as soon as you touch outside the keyboard to scroll the result list, the keyboard closes. That is very annoying and should be something that Unity allows me to decide whether it makes sense in the given context.
     
  11. CubeGameStudio

    CubeGameStudio

    Joined:
    Dec 1, 2019
    Posts:
    18
    The only solution (workaround) I found for this was to call this coroutine after the done button was pressed:

    Code (CSharp):
    1.  
    2.     private IEnumerator FocusInputFieldCoroutine(InputField input)
    3.     {
    4.         yield return null;
    5.  
    6.         input.Select();
    7.         input.ActivateInputField();
    8.     }
    This not prevent the keyboard close, but opens it just after closing, does anyone has a better solutions for this?
     
  12. MichaelFil

    MichaelFil

    Joined:
    Dec 15, 2015
    Posts:
    4
    Just remove coroutine (works for me on iPhone)

    Code (CSharp):
    1. messageInputField.onSubmit.AddListener(OnInputFieldSubmit);
    2.  
    3.         private void OnInputFieldSubmit(string message) {
    4.             if (!string.IsNullOrEmpty(message)) {
    5.                 // some stuff with message
    6.                 messageInputField.text = null;
    7.  
    8.                 messageInputField.Select();
    9.                 messageInputField.ActivateInputField();
    10.             }
    11.         }
     
  13. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    146
    What triggers the hiding of the keyboard? Shouldn't that be an event fired by Unity on end edit? Can I remove that listener?
     
  14. rainandgames

    rainandgames

    Joined:
    Aug 6, 2018
    Posts:
    12
    I've posted a feature request for this issue because I've tried everything to keep the keyboard open when focus is lost but I haven't been able to do it. This one fix would increase the usability and flow of my app more than anything else, so I'm really hoping we get a fix for it.
     
    NIOS and nvllap like this.
  15. unity_WHU2VAI6_H7PFg

    unity_WHU2VAI6_H7PFg

    Joined:
    Nov 19, 2019
    Posts:
    14
    It is ridiculous that such a commonly needed, basic feature like keeping the mobile keyboard opened is not natively supported in Unity.
     
  16. expandable

    expandable

    Joined:
    Feb 24, 2015
    Posts:
    24
    Still no Solution !?
    Come on Unity ! how can you neglect such basic details ?
     
  17. Necrospasm

    Necrospasm

    Joined:
    Apr 17, 2016
    Posts:
    9
    2022... We took people on the international space station on reusable rockets... but still there's no way to keep the mobile keyboard open! o_O:):p
     
    dnrkckzk2 likes this.
  18. nareshbishtasus

    nareshbishtasus

    Joined:
    Jun 11, 2018
    Posts:
    36
    Same here. Please fix this god dammit. Should have gone with UE5.
     
  19. amonraduyev

    amonraduyev

    Joined:
    Apr 12, 2020
    Posts:
    10
    Not sure if this will help or be a good solution but for IOS I ended up with an easier solution. Open Keyboard class from Xcode under Classes-UI. Search for "- (void)textInputLostFocus" function. Comment the codes inside this function. Thats all! Now whenever your inputfield loses its focus keyboard will remain visible. It will not close when you tap outside keyboard. You can close keyboard by tapping done or cancel button on top of keyboard.
     
  20. Afamuefuna

    Afamuefuna

    Joined:
    Sep 6, 2019
    Posts:
    13
    don't know if this is helpful but try this for android

    Code (CSharp):
    1. TouchScreenKeyboard.Android.consumesOutsideTouches = false
     
  21. Salma175

    Salma175

    Joined:
    May 8, 2022
    Posts:
    2
    Anything for IOS? Other than the hack mentioned above
     
  22. omari_barron

    omari_barron

    Joined:
    Feb 21, 2022
    Posts:
    1
    Any update on this for iOS
     
  23. Afamuefuna

    Afamuefuna

    Joined:
    Sep 6, 2019
    Posts:
    13
  24. standardcombo

    standardcombo

    Joined:
    Jun 28, 2012
    Posts:
    19
  25. jokigenki

    jokigenki

    Joined:
    Jul 19, 2014
    Posts:
    41
    I found a fairly horrible workaround for this. In my case, I wanted the field to be cleared each time the submit button was pressed, and then allow the user to enter a new word.

    I set up the input field as so:
    * Multiline field with newline instead of submit
    * Add an OnValueChanged listener
    * In the callback use text.EndsWith("\n") to check for the enter key being pressed (I guess I could have listened for OnKeyDown instead)
    * If so, use the word, and clear the field.

    Because it's technically not "submitting" the word, then the keyboard doesn't get deactivated.
     
  26. Genies_Dom

    Genies_Dom

    Joined:
    Oct 5, 2022
    Posts:
    2
    Unity did this? Jeez that's awesome.
     
  27. dofrenador

    dofrenador

    Joined:
    Apr 12, 2019
    Posts:
    1
    Any update on this for android or IOS?