How to call a function when mobile keyboard is "done"?

I originally made my app for desktop and this is how I handle a physical keyboard.

 AddTodoField.onEndEdit.AddListener(val =>
        {
                if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)  )
                AddTodo();
        });

With this, any time enter is hit, it calls a function which takes the inputfield text and does some stuff with it, and then blanks out the input field.

I tried changing my code to

if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter || TouchScreenKeyboard.done)  )

but it was too good to be true and threw a “an object reference is required to access non-static member TouchScreenKeyboard.done” error.

So I tried assigning TouchScreenKeyboard.done using get component(which I didn’t really expect to work, but it was worth a shot)

private TouchScreenKeyboard keyboard;
void Start () 
{
    keyboard = GetComponent<TouchScreenKeyboard>();
}

and it gave me the error “ArgumentException: GetComponent requires that the requested component ‘TouchScreenKeyboard’ derives from MonoBehaviour or Component or is an interface.”

I’ve found plenty of documentation for creating a new touchkeyboard, opening the keyboard, and then access “done” from that keyboard, but no documentation for accessing “done” from the touchkeyboard that automatically opens with InputFields.

Does anyone know how to do this? Any help is appreciated.

Hi,

Assign a reference to the touchscreen keyboard on your inputfield event and use that to assess if the event is done or not.

Here’s a starting point for you:

private TouchscreenKeyboard mobileKeys;

OnInputEvent()
{
mobileKeys = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.default, false);
}

//Then you can check if the event is done in your Update method or wherever needed
if (mobileKeys.done)

(The script should go on the input object)
Good luck!

It’s not the exact same thing - but if you’re using TMP_InputField, you can do this:

private void OnEnable()
{
var _inputField = GetComponent<TMP_InputField>();
_inputField.onTouchScreenKeyboardStatusChanged.AddListener(ProcessDonePressed);
}

void ProcessDonePressed(TouchScreenKeyboard.Status newStatus)
{
    Debug.Log("Done Pressed");
}

private void OnDisable()
{
    var _inputField = GetComponent<TMP_InputField>();
    _inputField.onTouchScreenKeyboardStatusChanged.RemoveListener(ProcessDonePressed);
}