dev-masih
dev-masih

Reputation: 4756

How to detect "done" Button in softkeyboard Unity3d

I use a InputField in my android app to get a string a soft keyboard pops up when i'm entering a string but now i want to call a function when user press "done" button in softkeyboard how can i do that?
Unity3d 4.6.2f1
done key

Upvotes: 2

Views: 7556

Answers (3)

Junior
Junior

Reputation: 91

Still works on 2019.3! I just had to change a few things to work with TMP_InputField.

Upvotes: 0

whatever0010011
whatever0010011

Reputation: 457

the best way i found is to subclass InputField. You can look into the source for the UnityUI on bitbucket. In that subclass you can access the protected m_keyboard field and check whether done was pressed AND not canceled, that will give you the desired result. Using "submit" of the EventSystem doesn't work properly. Even nicer when you integrate it into the Unity EventSystem.

Something like this: SubmitInputField.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine.Events;
using System;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;

public class SubmitInputField : InputField
{
    [Serializable]
    public class KeyboardDoneEvent : UnityEvent{}

    [SerializeField]
    private KeyboardDoneEvent m_keyboardDone = new KeyboardDoneEvent ();

    public KeyboardDoneEvent onKeyboardDone {
        get { return m_keyboardDone; }
        set { m_keyboardDone = value; }
    }

    void Update ()
    {
        if (m_Keyboard != null && m_Keyboard.done && !m_Keyboard.wasCanceled) {
            m_keyboardDone.Invoke ();
        }
    }
}

Editor/SubmitInputFieldEditor.cs

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;
using UnityEditor.UI;

[CustomEditor (typeof(SubmitInputField), true)]
[CanEditMultipleObjects]
public class SubmitInputFieldEditor : InputFieldEditor
{
    SerializedProperty m_KeyboardDoneProperty;
    SerializedProperty m_TextComponent;

    protected override void OnEnable ()
    {
        base.OnEnable ();
        m_KeyboardDoneProperty = serializedObject.FindProperty ("m_keyboardDone");
        m_TextComponent = serializedObject.FindProperty ("m_TextComponent");
    }


    public override void OnInspectorGUI ()
    {
        base.OnInspectorGUI ();
        EditorGUI.BeginDisabledGroup (m_TextComponent == null || m_TextComponent.objectReferenceValue == null);

        EditorGUILayout.Space ();

        serializedObject.Update ();
        EditorGUILayout.PropertyField (m_KeyboardDoneProperty);
        serializedObject.ApplyModifiedProperties ();

        EditorGUI.EndDisabledGroup ();
        serializedObject.ApplyModifiedProperties ();
    }
}

Upvotes: 6

user4794644
user4794644

Reputation:

You should be able to achieve the same by using this:

function Update () {
        Event e = Event.currrent;
        if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return)
            //Put in what you want here
    }

Upvotes: 0

Related Questions