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.
Answer by NicoVar · Oct 02, 2015 at 05:10 PM
link textI'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
5) 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)
6) Declare the IA_HideTouchScreenKeyboard() function at the beginning of your class:
[DllImport ("__Internal")]
private static extern void IA_HideTouchScreenKeyboard();
7) 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.
Answer by iit.vivek · May 20, 2016 at 04:05 PM
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 Vivek,
That is strange. Yesterday I re-used the same approach to another project and it worked fine.
I've added the $$anonymous$$eyboard.mm file I use to the original answer above: http://answers.unity3d.com/storage/attachments/70460-keyboardmm.zip for you to compare with yours. $$anonymous$$eep in $$anonymous$$d it can differ slightly if our versions are not the same.
Try to re-check your steps and make sure you haven't forgotten anything. I didn't experience the freeze. Can you debug on XCode and see where its freezing?
Thanks Nico for quick response! I cleaned up the project and tried your steps again and it worked! Super helpful!
Answer by icebear007 · Jun 15, 2016 at 09:18 PM
Hi,
Has anyone found a more elegant solution than this? I don't want to mess around with the mm file.
Thanks, Alex
Answer by karsnen · Oct 18, 2019 at 06:15 PM
Create a script (say Field.cs) that would have a text field [https://docs.unity3d.com/ScriptReference/UIElements.TextField.html] attached to it.
Now open [https://docs.unity3d.com/ScriptReference/TouchScreenKeyboard.Open.html] 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.
can you please elaborate on it? i'm facing the same issue. i want to build a crossword app and i have multiple input fields and the keyboard opens and closes when i navigate between them.
edit: well i ended up implementing my own custom keyboard.
Yes this answer could use some elaboration! it's not very clear what steps to take.
Your answer
Follow this Question
Related Questions
Unity 2017.3.0f3 - Input Field does not pop up TouchScreenKeyboard 0 Answers
Input field android problems 0 Answers
How to have rich text in an input field? 0 Answers
Unity 5.2.2 InputField caret bad horizontal position ios 1 Answer
How to disable native input field from Android TouchScreenKeyboard. 0 Answers