Author Topic: Setting focus on input widget in code  (Read 16229 times)

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Setting focus on input widget in code
« 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?

joreldraw

  • Guest
Re: Setting focus on input widget in code
« Reply #1 on: September 24, 2012, 01:51:32 AM »
void OnSelect (bool isSelected)
   {
      
      if (isSelected){};
         
      
      if (!isSelected) {};
      
   }

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Setting focus on input widget in code
« Reply #2 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting focus on input widget in code
« Reply #3 on: September 24, 2012, 08:31:02 AM »
UIInput.selected = true.

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Setting focus on input widget in code
« Reply #4 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.

PhilipC

  • Guest
Re: Setting focus on input widget in code
« Reply #5 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.

sjames

  • Guest
Re: Setting focus on input widget in code
« Reply #6 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...

sjames

  • Guest
Re: Setting focus on input widget in code
« Reply #7 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting focus on input widget in code
« Reply #8 on: April 17, 2013, 03:48:33 PM »
Don't change the UICamera.selectedObject. Set UIInput's selected property to true instead.

sjames

  • Guest
Re: Setting focus on input widget in code
« Reply #9 on: April 22, 2013, 09:11:33 PM »
Tried both...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting focus on input widget in code
« Reply #10 on: April 22, 2013, 09:42:16 PM »
Do you have "Use Keyboard" checked on your UICamera?

sjames

  • Guest
Re: Setting focus on input widget in code
« Reply #11 on: April 30, 2013, 11:59:56 AM »
lol. yes...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting focus on input widget in code
« Reply #12 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.

Oakshiro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Setting focus on input widget in code
« Reply #13 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? 

Oakshiro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Setting focus on input widget in code
« Reply #14 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.