Author Topic: Selecting UIInput field without text selection  (Read 5152 times)

Wumpee

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 53
    • View Profile
Selecting UIInput field without text selection
« on: October 18, 2014, 07:46:42 PM »
How can I set a UIInput object as active and not have the text selected?

I am trying to set up a key binding for the / key so that when you press it, the input field is selected, and a slash is in the input field already so you can continue typing, but the / always comes up selected with the highlight on it, which I don't want.

Doing this following doesn't work as expected:

  1. var input = gameObject.GetComponent<UIInput>();
  2. input.value = "/";
  3. input.selectionStart = 1;
  4. input.selectionEnd = 1;

I want it so that when I press / I can continue typing rather than having to press Enter, then type "/help" etc.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #1 on: October 19, 2014, 11:57:01 AM »
I handle that in Windward, and don't have to do anything special. Here's the Update() function of a script attached to my input field:
  1.         protected override void Update ()
  2.         {
  3.                 base.Update();
  4.  
  5.                 if (!UICamera.inputHasFocus)
  6.                 {
  7.                         if (Input.GetKeyDown(KeyCode.Slash) && !MyPlayer.HasBoundKey(KeyCode.Slash))
  8.                         {
  9.                                 input.isSelected = true;
  10.                         }
  11.                         else if (Input.GetKeyUp(KeyCode.R) && !MyPlayer.HasBoundKey(KeyCode.R))
  12.                         {
  13.                                 if (!string.IsNullOrEmpty(GameChat.lastPlayerPM))
  14.                                 {
  15.                                         input.isSelected = true;
  16.                                         input.value = "/pm " + GameChat.lastPlayerPM + ": ";
  17.                                 }
  18.                         }
  19.                 }
  20.         }

Wumpee

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #2 on: October 19, 2014, 04:51:36 PM »
When I try that exact code, I still end up with the entire text in the UIInput field selected automatically, so as soon as you start typing it overwrites what was inserted into the control.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #3 on: October 20, 2014, 11:16:14 PM »
I wonder if it's anything related to the NGUI changes I have in the Windward branch... I've just done a merge into the NGUI pro branch, if you have access to that, give it a try?

Wumpee

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #4 on: October 21, 2014, 07:23:19 AM »
I don't currently have access. Can I email you my asset store receipt to get access to the repository?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #5 on: October 22, 2014, 05:09:27 AM »
Unfortunately no. Pro access is a different license. I can send you an updated unitypackage to try though. Toss me an email with the OR# and I'll hook you up.

Wumpee

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #6 on: October 22, 2014, 06:05:57 AM »
Thank you... I've sent you a PM.

hrlarsen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 35
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #7 on: October 23, 2014, 07:42:07 AM »
Did you guys resolve this, because Im facing a similar issue, where I am trying to create a simple text editor where you can press bold, italic etc. and it should be so that you just continue from where you left the text caret. I also get the whole text selected when setting input.isSelected = true


hrlarsen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 35
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #8 on: October 23, 2014, 08:17:58 AM »
Nevermind, found a solution. I forgot to set the variable input.selectAllTextOnFocus = false;

Wumpee

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #9 on: October 23, 2014, 04:50:09 PM »
Thanks for mentioning that hrlarsen.

That fixes the issue I had too.

Zylann

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #10 on: August 16, 2016, 06:06:09 AM »
Sorry to necro, but I have the exact same issue for the same use case, and none of the above worked for me in 3.9.9.

I need both: when the user clicks or hits a shortcut, the whole text must be selected.
But, in some cases, I want to focus the input and append text to it without selecting the text.

I tried this:

  1.         bool temp = input.selectAllTextOnFocus;
  2.         input.selectAllTextOnFocus = false;
  3.  
  4.         input.isSelected = true;
  5.         input.value = text;
  6.  
  7.         input.selectAllTextOnFocus = temp;
  8.  

But it doesn't works.

Actually I found that the text gets selected one frame after, from the Update() callback in UIInput (only place selectAllTextOnFocus is used), making it needlessly hard to temporarily disable it when setting a text prefix :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Selecting UIInput field without text selection
« Reply #11 on: August 17, 2016, 07:22:43 AM »
  1. input.selectAllTextOnFocus = false;
  2. input.isSelected = true;
  3. input.value = text;
  4. UIEventDelegate.Get(input.gameObject).on Select = delegate (GameObject go, bool isSelected)
  5. {
  6.     if (!isSelected) input.selectAllTextOnFocus = true;
  7. };
  8.  
Note I had to spell "on Select" with a space because this forum rejects the post without the space. You will want to remove it though.