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

jeffc42

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

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 #16 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).

jeffc42

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

jeffc42

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

skullthug

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

zeus_82

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Setting focus on input widget in code
« Reply #20 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.
« Last Edit: May 18, 2016, 06:51:12 PM by zeus_82 »