Author Topic: iPhone keyboard input  (Read 5973 times)

ozRocker

  • Guest
iPhone keyboard input
« on: April 24, 2012, 03:23:55 AM »
I just purchased NGUI and I love it!  Only realised how awesome it was when I started using it.
I have a question about using the iPhone keyboard (or any mobile device keyboard) with the input widgets.  I'm not sure what the best way is to process text entered into the input field.  The events OnInput and OnKey work in the Unity editor but don't get fired off in the iPhone.  So instead I used "TouchScreenKeyboard.visible"  It is a boolean though, not an event so I have to keep polling in the Update() function to see if it changes.  This is what I've done (blnKeyboard is initialised to FALSE):

  1.                 if (!blnKeyboard) {
  2.                         if (TouchScreenKeyboard.visible) {
  3.                                 blnKeyboard = true;
  4.                         }
  5.                 }
  6.                 if (blnKeyboard) {
  7.                         if (!TouchScreenKeyboard.visible) {
  8.                                 Debug.Log("Process new text here");
  9.                                 blnKeyboard = false;
  10.                         }
  11.                 }                      
  12.  

It works but it doesn't tell me which input field was changed.  It just tells me if the keyboard was closed.  I would have to check all input fields to see which ones had their values changed.  Its not really a problem, but I'm just wondering if there's a better way to do this.  This applies to input fields in Unity in general so its not specific to NGUI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: iPhone keyboard input
« Reply #1 on: April 24, 2012, 02:05:21 PM »
Why don't you just use UIInput? It does all this for you.

ozRocker

  • Guest
Re: iPhone keyboard input
« Reply #2 on: April 25, 2012, 01:32:34 AM »
Ahh, I digged deeper and disovered the OnSubmit() event in UIInput which detects Enter/Return in the Unity Editor and also keyboard closing on the iPhone.  I knew it was something simple!

DCalabrese

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: iPhone keyboard input
« Reply #3 on: April 28, 2014, 05:40:15 PM »
I'm also attempting to detect when the Keyboard Close button is pressed, as currently it does not appear to set the UIInput field as inactive (so if you close the keyboard then tap the same input field again, the keyboard does not re-open as it still thinks it's active).

ozRocker, could you possibly post the code you used? I'm looking in UIInput and I'm not even seeing a OnSubmit function, just a List called onSubmit.

As another way of doing this, I did try accessing the TouchScreenKeyboard in mKeyboard during the Update phase, however it didn't work at all. Here's what I tried:
  1.                 if(isSelected && mKeyboard != null)
  2.                 {
  3.                         if(!mKeyboard.active)
  4.                         {
  5.                                 Submit();
  6.                                 mKeyboard = null;
  7.                                 isSelected = false;
  8.                                 mCached = "";
  9.                         }
  10.                 }
  11.  

kamend

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: iPhone keyboard input
« Reply #4 on: May 02, 2014, 04:05:38 AM »
I have the same problem, when pressing the "close keyboard button" I could not open the keyboard anymore or it takes couple of presses to do this..  :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: iPhone keyboard input
« Reply #5 on: May 02, 2014, 09:15:43 AM »
When the keyboard is closed, you should get mKeyboard.wasCanceled set to 'true', which is handled in the Update() function. Maybe mKeyboard.done is not set properly by Unity in some cases? You can try changing the Update function to this:
  1.         void Update()
  2.         {
  3.                 if (mKeyboard != null && isSelected)
  4.                 {
  5.                         string text = mKeyboard.text;
  6.  
  7.                         if (mCached != text)
  8.                         {
  9.                                 mCached = text;
  10.                                 value = text;
  11.                         }
  12.  
  13.                         if (mKeyboard.done || !mKeyboard.active)
  14.                         {
  15. #if !UNITY_3_5
  16.                                 if (!mKeyboard.wasCanceled)
  17. #endif
  18.                                         Submit();
  19.                                 mKeyboard = null;
  20.                                 isSelected = false;
  21.                                 mCached = "";
  22.                         }
  23.                 }
  24.         }
Related: http://www.tasharen.com/forum/index.php?topic=1635.0