Author Topic: UIInput Caret Manipulation  (Read 6541 times)

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
UIInput Caret Manipulation
« on: May 13, 2014, 02:21:10 PM »
I've spent way too much time on this, so I finally am resorting to the forums.   I did try to resolve this myself first :p

I have a UIInput that is disabled.  When a user pressed the enter key, it enabled the UIInput and associated widget for adding text.  Works fine.

I would like to be able to press a different key, have the UIInput be enabled, and that key already be in the input field.  Sounds simple.  I would like this character to not be highlighted, because that's what happens every time at the moment.  If it's highlighted, the next character you type just deletes it, and well, it's like it was never there.

Here's what I've been trying, in various forms.  I looked at what pressing the right/left arrow keys do, and I fail to see the magic of what I'm missing.  Assistance would be appreciated.

  1. else if (!inputChat.gameObject.activeSelf && Input.GetKeyUp(KeyCode.Slash))
  2.             {
  3.                 inputChat.gameObject.SetActive(true);
  4.                 inputChat.isSelected = true;
  5.                 inputChat.value = "/";
  6.                 inputChat.cursorPosition = 1;
  7.                 inputChat.selectionStart = 1;
  8.                 inputChat.selectionEnd = 1;
  9.                 inputChat.UpdateLabel();
  10.             }

So I've tried different order of what's above, messing around with it... and well, enough trial and error, here I am at the forums.  Like I said, it works fine except the character is highlighted when it's added.

Thanks,
Alex

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput Caret Manipulation
« Reply #1 on: May 14, 2014, 06:13:58 AM »
Setting selectionStart and selectionEnd already calls UpdateLabel() inside. Same with cursorPosition. Other than that what you're doing is fine, but I'm questioning the part that actually enables the object beforehand. Why do this? Why not have the input field be there already, and then simply select it like you're doing when pressing a specific key?

Also keep in mind that UIKeyBinding script can do all this for you, minus the part that sets the input's value to "/".

I just tried your script and it works fine by the way. But not with KeyCode.Slash. I tried it with KeyCode.M. This is the full script I used:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public UIInput inputChat;
  6.  
  7.         void Update ()
  8.         {
  9.                 if (Input.GetKeyDown(KeyCode.M))
  10.                 {
  11.                         if (inputChat.isSelected) return;
  12.                         inputChat.gameObject.SetActive(true);
  13.                         inputChat.isSelected = true;
  14.                 }
  15.         }
  16. }
The letter 'm' ends up in the input field automatically, since it's processed after Update().

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #2 on: May 14, 2014, 07:02:29 AM »
The letter shows up fine, but it's highlighted is the issue... so if you type anything else immediately after, it will overwrite the letter/character provided.

The idea / goal is:

version 1 - works fine
You're playing the game.  The chat input is completely hidden.
You hit enter.
Chat input appears, cursor blinking, ready for input.

version 2
You're playing game.  The chat input is completely hidden.
You hit slash, as all commands are IRC-esque and begin with a slash.
Chat input appears, slash is first character, cursor is at position after slash, slash obviously is not highlighted.  Ready for additional input.

So, the uiInput doesn't have focus yet when the slash is processed, it's being used outside of the uiInput to:
activate disabled uiInput
add slash to uiInput
move caret to position 1, past slash character
put focus on uiInput

The character is highlighted is the problem (sorry if I'm redundant a lot).

I'll mess around with it some more.
« Last Edit: May 14, 2014, 07:08:02 AM by Yukichu »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput Caret Manipulation
« Reply #3 on: May 14, 2014, 07:03:57 AM »
It isn't highlighted in my case. Pressing 'm' twice results in "mm" being printed.

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #4 on: May 14, 2014, 07:08:29 AM »
Posted an edit above, man you're quick.

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #5 on: May 14, 2014, 07:10:26 AM »
I'll mess with it some more, I think I understand (or at least have some new ideas.)

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #6 on: May 14, 2014, 03:02:55 PM »
ArenMook, you are awesome.  You are like a support genius. 

I am convinced you've cloned yourself so one of your clones can just reply to forum posts.

Anyhow, it's fixed.  I was checking Input.OnKeyUp instead of Input.OnKeyDown ...  so the uiInput must've been capturing the key inputs on key down and not up, so you are very correct... I do not have to actually set the character explicitly in the code.  It just shows up on its own.

Very simple.  I was completely overdoing it.

Thank you again.

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #7 on: August 16, 2014, 04:28:32 PM »
Not a necro post, this seriously just stopping working as expected.

  1.             if (!m_uiInputChat.gameObject.activeSelf && Input.GetKeyDown(KeyCode.Slash))
  2.             {
  3.                 Debug.Log("Input chat activating due to '/' input");
  4.                 if (m_uiInputChat.isSelected)
  5.                     return;
  6.                 m_uiInputChat.gameObject.SetActive(true);
  7.                 m_uiInputChat.isSelected = true;
  8.             }

Code hasn't changed since I resolved the issue in here previously, have updated NGUI a few times, now it's not working out so well.

Any ideas?

Trying to get a character (a slash in this instance) to appear in the input box when you press the same button.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput Caret Manipulation
« Reply #8 on: August 17, 2014, 09:11:16 AM »
That's a result of a work-around for a bug in Unity with "hidden input"-type on-screen keyboards. Find line 619 of UIInput.cs that's the "return;" statement right after UpdateLabel();:
  1.                                 UpdateLabel();
  2.                                 return;
Change it to:
  1.                                 UpdateLabel();
  2.                                 if (string.IsNullOrEmpty(Input.inputString)) return;

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #9 on: August 17, 2014, 02:42:48 PM »
Worked like a charm.

Thank you!

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #10 on: August 25, 2014, 01:01:07 PM »
The issue that would never end...

I have found that as started earlier, the change you helped out with worked; however, I've found out it only works in the editor.

x86 build, at runtime, this does not actually work.  It gives focus to the UIInput but the character I want added isn't there.

To ensure this wasn't some hodgepodge part of my code, I made a new project, imported NGUI, added only a UIInput, and wrote a very simple code:

  1. if (Input.GetKeyDown(KeyCode.Slash))
  2. {
  3.     input.isSelected = true;
  4. }

That's it.  I'm not even disabling the UIInput or anything.  It gives it focus but I want the typed character to show up in it too.  Adding it via code highlights it, so if you were to type (slash-key) then another letter, the slash key is overwritten.

Haven't tried other platforms, just PC right now.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput Caret Manipulation
« Reply #11 on: August 26, 2014, 03:00:26 AM »
Input handling is terrible in Unity, so I'm not surprised that there are more issues. Well, my suggestion is do it via code instead. When a key is pressed, set isSelected and change the input field's text, then set UIInput.selectionStart and selectionEnd to the end so that nothing is highlighted.

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #12 on: August 26, 2014, 06:43:23 AM »
Well, not the most fun thing to figure out.

Changing the selectionStart / selectionEnd did nothing, as they were already being set during the OnSelect event, so it was too late... or something, I don't know.  CursorPosition did nothing too.

I had to update the UIInput Update to where it is selected the first time, change the mSelectionStart equal to mSelectionEnd, only when the platform is WindowsPlayer.

Just another custom thing to add I suppose, ah well.  Figured it out, I hope... until I make an OSX build and nothing works again, or webplayer, or whatever.  So annoying.

Thanks again for the assistance.

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: UIInput Caret Manipulation
« Reply #13 on: August 26, 2014, 06:54:14 AM »
Could you please change the Update() in UIInput to protected virtual?

I'd like to just override it instead of having to make updates every time I, well, update.  Will make things much smoother.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput Caret Manipulation
« Reply #14 on: August 27, 2014, 03:51:10 AM »
Sure thing.