Keep mobile keyboard open

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

1 Like

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

public void DeactivateInputField()
        {
            // Not activated do nothing.
            if (!m_AllowInput)
                return;

            m_HasDoneFocusTransition = false;
            m_AllowInput = false;

            if (m_TextComponent != null && IsInteractable())
            {
                if (m_WasCanceled)
                    text = m_OriginalText;

                if (m_Keyboard != null)
                {
                    m_Keyboard.active = false;
                    m_Keyboard = null;
                }

                m_CaretPosition = m_CaretSelectPosition = 0;

                SendOnSubmit();

                Input.imeCompositionMode = IMECompositionMode.Auto;
            }

            MarkGeometryAsDirty();
        }

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.

Hi Brathnann,

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

void OpenKeyboard()
{
    keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, true, false, false, false);
}
// ... 
void Update()
{
        if (keyboard != null && keyboard.done)
        {
               //  check keyboard ...
        }
        // ... 
}

to create a keyboard and check it. I’ll have a look at the input field methods.

Alex

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.

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

I’m at work, but I’ll glance at the inputfield code when I get home much later today.

Did you figure out a solution to this?

Hi there , is there any solutions for this ?

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.

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.

The only solution (workaround) I found for this was to call this coroutine after the done button was pressed:

    private IEnumerator FocusInputFieldCoroutine(InputField input)
    {
        yield return null;

        input.Select();
        input.ActivateInputField();
    }

This not prevent the keyboard close, but opens it just after closing, does anyone has a better solutions for this?

Just remove coroutine (works for me on iPhone)

messageInputField.onSubmit.AddListener(OnInputFieldSubmit);

        private void OnInputFieldSubmit(string message) {
            if (!string.IsNullOrEmpty(message)) {
                // some stuff with message
                messageInputField.text = null;

                messageInputField.Select();
                messageInputField.ActivateInputField();
            }
        }

What triggers the hiding of the keyboard? Shouldn’t that be an event fired by Unity on end edit? Can I remove that listener?

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.

2 Likes

It is ridiculous that such a commonly needed, basic feature like keeping the mobile keyboard opened is not natively supported in Unity.

Still no Solution !?
Come on Unity ! how can you neglect such basic details ?

2022… We took people on the international space station on reusable rockets… but still there’s no way to keep the mobile keyboard open! :eyes::):stuck_out_tongue:

1 Like

Same here. Please fix this god dammit. Should have gone with UE5.

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.

don’t know if this is helpful but try this for android

TouchScreenKeyboard.Android.consumesOutsideTouches = false