Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Official Important Message Related to the TextField Refactor for 2022.1+.

Discussion in 'UI Toolkit' started by HugoBD-Unity, Nov 19, 2021.

  1. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    Hello @johfirefly!

    - The focus handling is tricky for TextField, but I briefly explained how it works in this post.
    - As for adding custom behaviour to the TextField on KeyDown, have a look at this post. It's a code sample showing how to achieve it.

    Let me know if it helps!

    Also, yes it's probably best to create a new thread as this one is starting to get lengthy
     
  2. johfirefly

    johfirefly

    Joined:
    Aug 21, 2018
    Posts:
    15
    I roughly understood how focus works, it helped me create a "workaround" for returning focus to the input field after pressing "return".

    Adding commands:
    evt.StopPropagation();
    evt.PreventDefault();
    to processing unit evt.keyCode == KeyCode.Enter (as in the example in the links)
    did not help to avoid defocusing the input field

    The "workaround" I'm currently using is:
    Code (CSharp):
    1. if (ev.keyCode == KeyCode.Return)
    2. {
    3.     <any code>
    4.  
    5.    StartCoroutine(ReFocus());
    6. }
    7.  
    8. IEnumerator ReFocus()
    9.     {
    10.         TextField.Blur();
    11.         yield return null;
    12.         TextField.Focus();
    13.       }
     
  3. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    225
  4. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    105
    Hi,

    I noticed that when you press TAB in a multiline textfield, the textfield doesn't lose focus but a tab is added to the text. I think the default behaviour should be to go to the next focusable element like happens when multiline=false. Could that be changed? Or is there a reason that it was implemented that way?

    Thanks
    Chloë
     
  5. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    Hi @ChGuidi !

    We don't expose an API to control this behaviour, but I believe this can be achieved by adding a bit of custom logic. Have a look at this post to intercept events before TextField processes them. It would look something like this:

    Code (CSharp):
    1. myField.RegisterCallback<KeyDownEvents>((evt) => {
    2.             if (evt.modifiers == EventModifiers.Shift && (evt.keyCode == KeyCode.Tab || evt.character == '/t'))
    3.             {
    4.                 // Focus logic for previous
    5.  
    6.                 evt.StopPropagation();
    7.                 evt.PreventDefault();
    8.             }
    9.             else if (evt.keyCode == KeyCode.Tab || evt.character == '/t')
    10.             {
    11.                 // Focus logic for next
    12.  
    13.                 evt.StopPropagation();
    14.                 evt.PreventDefault();
    15.             }
    16. }, TrickleDown.TrickleDown);
     
  6. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    Here's how we handle the focus delegation internally. Unfortunately, it's not public, but you could use it through C# reflection.
     
  7. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    105
    Ok thanks, I'll give it a try! Is there a reason that this is not the default behaviour?

    [Edit] I tried Shift+TAB which should work if I understand the code correctly, but this doesn't give the focus to the next element.
     
    Last edited: Jan 19, 2023
  8. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    The reasoning behind this is to emulate the same behaviour as IMGUI. I agree, not the best reason. What we could do is expose some configuration to enable/disable tab navigation on the TextField. I'll log something for this.

    Thanks!
     
    ChGuidi likes this.
  9. Karmahero

    Karmahero

    Joined:
    Feb 11, 2012
    Posts:
    11
    Is "textElement.selection.isSelectable = true;" designed to work for Labels inside a VisualTreeAsset template used in a ListView? I've tried setting this, but labels are not selectable:

    history.makeItem = () => itemTemplate.CloneTree().contentContainer;

    history.bindItem = (e, i) =>
    {
    var message = e.Q<Label>("item-detail-message");
    message.text = chatItems.Message;
    message.selection.isSelectable = true;

    };

    Are there additional steps required?
     
  10. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
  11. Karmahero

    Karmahero

    Joined:
    Feb 11, 2012
    Posts:
    11
    Ok, Thanks. I'll try some testing with it as maybe there was something else interfering in its layout.
     
    Last edited: Apr 14, 2023
  12. Jimb4ik

    Jimb4ik

    Joined:
    Jun 3, 2018
    Posts:
    8
    Hello! Any updates on this? Maybe I missed something...

    Thanks a lot
     
  13. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    Hi @Jimb4ik!

    Softkeyboard improvements have landed and have been backported to 2022.2. Unfortunately, we did not get to rich text support for TextField yet.
     
    jGate99 likes this.
  14. Jimb4ik

    Jimb4ik

    Joined:
    Jun 3, 2018
    Posts:
    8
    Thank you very much for response
     
  15. Jimb4ik

    Jimb4ik

    Joined:
    Jun 3, 2018
    Posts:
    8
    I've tested 2022.2 but InputField still has no possibility to copy/paste/selectAll and other native options when running on iOS device. Is it planned to implement or I should replace my whole UI with UItoolkit?

    Thank you in advance
     
  16. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    Hi @Jimb4ik, this is a known issue but is owned by the platform team. Ideally, we want the native context menu to pop up. You could ask for more info on this thread.