Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: phenotype on September 23, 2012, 08:01:16 PM

Title: Setting focus on input widget in code
Post by: phenotype on September 23, 2012, 08:01:16 PM
I've been trying to figure out how to set focus on an input widget in code. I have tried Sending a OnClick, OnSelect and OnPress event to the input gameobject but no luck. I have tried setting the camera's selectobject to the input but all that I get is a pipe inserted into the input field. The input still does not have focus. Has anyone done this?
Title: Re: Setting focus on input widget in code
Post by: joreldraw on September 24, 2012, 01:51:32 AM
void OnSelect (bool isSelected)
   {
      
      if (isSelected){};
         
      
      if (!isSelected) {};
      
   }
Title: Re: Setting focus on input widget in code
Post by: phenotype on September 24, 2012, 08:08:07 AM
thanks jorel but I don't understand how that code gives the input control focus. That method gets called when the control gets focus but that is not what I am trying to do.
Title: Re: Setting focus on input widget in code
Post by: ArenMook on September 24, 2012, 08:31:02 AM
UIInput.selected = true.
Title: Re: Setting focus on input widget in code
Post by: phenotype on September 24, 2012, 10:50:59 AM
That doesn't work for me. I just get a pipe character in my input but the input itself does not have focus. If I start typing nothing appears in the input. Behind the scenes the selected property is doing exactly what I was doing manually in code, setting the camera's selected object, which didn't work for me either.
Title: Re: Setting focus on input widget in code
Post by: PhilipC on September 24, 2012, 02:21:03 PM
Hmm that should work. I'm not 100% sure maybe something is setting the selected input back to null. Try putting a Debug in UICamera 667 and see if you are even getting any input string to begin with.
Title: Re: Setting focus on input widget in code
Post by: sjames on April 16, 2013, 11:00:16 PM
I have this issue too. Setting the UICamera.selectedObject doesn't work, neither does setting .selected on the uiInput itself. I tried changing max chars as well. Pretty frustrating...
Title: Re: Setting focus on input widget in code
Post by: sjames on April 17, 2013, 03:16:41 PM
I have ensured that the selectedObject is the input, but it doesn't seem to be receiving the keyboard input. Anyone?
Title: Re: Setting focus on input widget in code
Post by: ArenMook on April 17, 2013, 03:48:33 PM
Don't change the UICamera.selectedObject. Set UIInput's selected property to true instead.
Title: Re: Setting focus on input widget in code
Post by: sjames on April 22, 2013, 09:11:33 PM
Tried both...
Title: Re: Setting focus on input widget in code
Post by: ArenMook on April 22, 2013, 09:42:16 PM
Do you have "Use Keyboard" checked on your UICamera?
Title: Re: Setting focus on input widget in code
Post by: sjames on April 30, 2013, 11:59:56 AM
lol. yes...
Title: Re: Setting focus on input widget in code
Post by: ArenMook on April 30, 2013, 10:54:10 PM
Then there is no reason why it shouldn't work. Create a new scene, create an input field, try typing.

In Starlink I use the following code to automatically select the input field when Enter is pressed:

  1. void Update ()
  2. {
  3.         if (Input.GetKeyUp(KeyCode.Return))
  4.         {
  5.                 if (!mIgnoreNextEnter) input.selected = true;
  6.                 mIgnoreNextEnter = false;
  7.         }
  8. }
Coincidently the very same code is used in the Chat example that comes with NGUI.
Title: Re: Setting focus on input widget in code
Post by: Oakshiro on July 30, 2013, 03:15:47 AM
I have exactly the same error. When I hit on enter on the Name field it calls a method that should activate the next field (email), but it only prints a pipe on the email field and when you continue typing you do it on the Name field instead!

How can we do this? 
Title: Re: Setting focus on input widget in code
Post by: Oakshiro on July 30, 2013, 03:27:57 AM
It seems that when we change the input.selected value the camera restores the input immediately after we change it. I found a workaround to make this work.

I use a UIInput reference to detect if we should change focus, and when we launch the "field Changed" method, we set it. Later on, on the update, we force set it until the UICamera.selectedObject equals the desired UIInput.

Here is the code:


  1.  
  2. private UIInput selectedInput = null;
  3. ..
  4. ..
  5. ..
  6.  
  7. public void OnNameChange()
  8. {
  9.         UICamera.selectedObject = passInput.gameObject;
  10.         selectedInput = passInput;
  11. }
  12.  
  13.  
  14. void Update ()
  15. {                                                      
  16.         if (UICamera.selectedObject != null && selectedInput != null)
  17.         {
  18.                  if (UICamera.selectedObject != selectedInput.gameObject)
  19.                  {
  20.                         selectedInput.text = ""; //avoid showing the caret
  21.                         selectedInput.selected = true;
  22.                  }
  23.         }
  24. }
  25.  
Title: Re: Setting focus on input widget in code
Post by: jeffc42 on April 11, 2014, 06:23:06 PM
I know this thread is old, but I have the same problem.  Nothing in this thread has worked for me, including Oakshiro's hack.  I simply cannot find any way to programmatically give keyboard focus to a UIInput.  I'm using 3.5.4 r2.
Title: Re: Setting focus on input widget in code
Post by: ArenMook on April 12, 2014, 04:40:30 AM
The process is easier now. Just attach the UIKeyBinding script to the input field. Set it to react to Return, and make it Select the object, not press it (which is the default setting).
Title: Re: Setting focus on input widget in code
Post by: jeffc42 on April 12, 2014, 05:47:14 PM
I'm afraid that doesn't do what I want.  I have an input field and a collection of buttons.  When one of the buttons is clicked, I want the keyboard focus to go to the input field.  I set isSelected on the input field to true, as described earlier in this thread (after the name change from "selected", of course), but that doesn't move the keyboard focus.  I even tried faking a mouse click on the input field, without success.
Title: Re: Setting focus on input widget in code
Post by: jeffc42 on April 12, 2014, 07:33:42 PM
OK, I figured out how to make it work.  Simply setting isSelected to true does not work because the button being clicked is in the middle of getting the input focus.  To make it work, I created a trivial method to set isSelected to true and then used Invoke() to execute it after a 0.1 second delay.  The button has finished getting the focus by then, so giving it to the input field at this point now sticks.
Title: Re: Setting focus on input widget in code
Post by: skullthug on June 09, 2014, 11:53:26 PM
JEFFC42 oh christ THANK YOU.
I was having this exact problem and was going batshit for it. But after trying your solution of adding a 0.1 second delay everything just started working.

I basically took the code from OnEnable in UIButtonKeys and invoke it as another method after .1 seconds.
Title: Re: Setting focus on input widget in code
Post by: zeus_82 on May 18, 2016, 04:58:01 PM
UIInput.selected = true.

This would work in most of cases. But sometimes it would not work because it is called at the same frame when user tapped on other widget.
That's why you need to start a coroutine and invoke that line on next frame, something like this :
  1. IEnumerator DelayedSelectInput() {
  2.     yield return null;
  3.     uiInputInstance.isSelected = true;
  4. }

and invoke that coroutine from place of usage :
  1. StartCoroutine(DelayedSelectInput());

Hope this helps.