- Home /
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.
Answer by herDev · Mar 13, 2017 at 01:15 PM
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!
Oh. Oh wow. I actually got it working. I was almost about to give up. Here's the new code:
private TouchScreen$$anonymous$$eyboard mobile$$anonymous$$eys;
public void OnInputEvent()
{
mobile$$anonymous$$eys = TouchScreen$$anonymous$$eyboard.Open("", TouchScreen$$anonymous$$eyboardType.Default, false);
}
void Start ()
{
AddTodoField.onEndEdit.AddListener(val =>
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$eypadEnter) || mobile$$anonymous$$eys.done )
AddTodo();
});
I was kind of confused. I thought OnInputEvent might have been a built-in Unity function(it's not). And I had to fix some capitalization(no big deal). Then I wasn't sure what I was supposed to do with the OnInputEvent() function. I checked my Input field and it already had a Unity script called "Event Trigger" I had added to it with an event type of "Select". So I aded OnInputEvent() to that and everything worked.
Thank you so much for your answer! I couldn't have done it without you.
Great, glad you got it working! Best of luck with the app
Answer by levilais · Oct 22, 2021 at 09:41 PM
It's not the exact same thing - but if you're using TMP_InputField, you can do this:
private void OnEnable() { var _inputField = GetComponent(); _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);
}
This worked for me. Thanks. (For anyone else, here's the full code I ended up using to check specifically for the Status.Done)
public TMP_InputField inputField;
Awake()
private void OnEnable() {
inputField.onTouchScreenKeyboardStatusChanged.AddListener(ProcessDonePressed);
}
private void OnDisable() {
inputField.onTouchScreenKeyboardStatusChanged.RemoveListener(ProcessDonePressed);
}
void ProcessDonePressed(TouchScreenKeyboard.Status newStatus) {
if (newStatus == TouchScreenKeyboard.Status.Done) {
Debug.Log("Done Pressed");
}
}
Your answer
Follow this Question
Related Questions
Mobile Keyboard Scales Screen 1 Answer
Unity Terrain texture issue on android device 1 Answer
Disabling the inputfield above the keyboard or disable it all 0 Answers
Keep input field caret active when focused on mobile devices in addition to native input field caret 0 Answers
TMP InputField does not fire onTouchScreenKeyboardStatusChanged event when keyboard becomes visible 0 Answers