iOS TouchScreenKeyboard closes and open when tapping on another InputField

I have several InputFields on a scene. When I tap the first field, the iOS TouchScreenKeyboard popups up. When I tap any other field, the keyboard closes and opens instead of staying opened.

This is extremely annoying.

I’ve found no way to keep the keyboard open manually at all times, nor to avoid it being closed when an InputField looses focus.

Any ideas or workarounds?

Thanks.

[70460-keyboardmm.zip|70460]I’ve worked around this by tapping into XCode and changing Unity’s original behavior regarding UI. Not sure if this would work on other situations, but it’s good enough for me.

Here’s what I did:

On Keyboard.mm

  1. Search for “- (void)hide” (line 229 on my case) and edit such function to add a return so its ignored, like so:

    • (void)hide
      {
      // modified by nico
      return;

      [self hideUI];
      _done = YES;
      }

  2. Right below such function, create another with a different name:

    • (void)IA_hide
      {
      [self hideUI];
      _done = YES;
      }
  3. Search for function UnityKeyboard_Hide() and above create another function like so:

    extern “C” void IA_HideTouchScreenKeyboard()
    {
    // do not send hide if didnt create keyboard
    // TODO: probably assert?
    if(!_keyboard)
    return;

     [[KeyboardDelegate Instance] IA_hide];
    

    }

  4. Replace “[self hide];” with “[self IA_hide];” on the following functions: textFieldShouldReturn, textInputDone and textInputCancel.

On UnityInterface.h

  1. Search for “UnityKeyboard_Hide” (line 302 on my case) and add the following row below:

    void IA_HideTouchScreenKeyboard();

On your Unity C# script (can easily be ported to JS)

  1. Declare the IA_HideTouchScreenKeyboard() function at the beginning of your class:

    [DllImport (“__Internal”)]
    private static extern void IA_HideTouchScreenKeyboard();

  2. Then, simply call IA_HideTouchScreenKeyboard() whenever you want to close the keyboard.

Keep in mind Keyboard will not close automatically now when you, for example, tap outside the InputField. But it will close if you tap the close button on the keyboard or hit the Done button. So, you may want to call IA_HideTouchScreenKeyboard() on your InputField End Edit event, and on any other situation where you may want to close the keyboard.

I’m sure there’s a much more elegant way to do this rather than simply blocking the keyboard hiding entirely, but I don’t have much time and it works on my case.

Hope this helps someone.

Hi @NicoVar ! Thank you so much for such a detailed solution! This was exactly what I was struggling with and your solution has almost helped me. I am just stuck at one place, as soon as I call IA_HideTouchScreenKeyboard() to close the Keyboard, my app hangs and doesn’t proceed. It is just stuck.

If I don’t call that function, keyboard is always visible in my app post my text input scene.

Can you help me look for what might be the issues? I am using latest Unity (5.3.4) and tested both on iOS 8.4 and 9.1.

Really appreciate your help!

Thanks
Vivek

Hi,

Has anyone found a more elegant solution than this? I don’t want to mess around with the mm file.

Thanks,
Alex

Create a script (say Field.cs) that would have a text field [Unity - Scripting API: TextField] attached to it.

Now open [Unity - Scripting API: TouchScreenKeyboard.Open] with the required parameters.

Once it is open, at any moment in time if you want to change the text field - just change the TextField component attached to the Field.cs

In that way, you don’t have to close the TouchScreenKeyboard & just switch the text fields.

So after a couple days of work 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.