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. Dismiss Notice

Any way to prevent select all upon focusing a TextField?

Discussion in 'UI Toolkit' started by herra_lehtiniemi, Jan 20, 2021.

  1. herra_lehtiniemi

    herra_lehtiniemi

    Joined:
    Feb 12, 2017
    Posts:
    133
    Is there any way to prevent TextField from selecting all content upon focus? Ideally - for my case - it would be possible to configure the caret so that it would be placed directly to where the user clicks upon focusing.

    I realize this is not always desired, but I would like to have a way to configure this behaviour (like TextField.singleClickSelectsAll (which would default to true like it does now) but could be made false to disable this behavior. Or preferably even more specific configuration when wanted.

    There was a workaround for IMGUI where someone found out that Unity does not select all when caret color is transparent. The workaround made the caret transparent upon focus and then restored it. However, it's not currently possible to change caret color (at least not yet in preview 13). Here's the original idea: https://stackoverflow.com/questions...mmediate-gui-from-selecting-all-text-on-click

    Any ideas welcomed!
     
    Nexer8, achimmihca and rigidbuddy like this.
  2. herra_lehtiniemi

    herra_lehtiniemi

    Joined:
    Feb 12, 2017
    Posts:
    133
    If this is not possible, I'd like to submit this as a feature request. Desired behavior would be that I could click on a TextField and cursor would be immediately positioned where I click, without selecting all text (like currently). This should be behind a boolean setting so current behavior would be preserved.
     
  3. herra_lehtiniemi

    herra_lehtiniemi

    Joined:
    Feb 12, 2017
    Posts:
    133
    Or is there some other place to submit feedback/improvement requests?

    To have a boolean like this:
    Code (CSharp):
    1. TextField.selectAllUponFocus = false
    would be perfect. It should remove the behavior that all TextField content is selected when I focus the element. Instead, when this would be false, caret would be directly moved to the position where I clicked without any selection. Currently first click selects all.
     
    rigidbuddy likes this.
  4. vejab

    vejab

    Joined:
    Dec 21, 2021
    Posts:
    85
    It's one year later, is there an easy way to do this now?
     
    achimmihca likes this.
  5. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    266
    Is there at least a workaround for this?
    For example, one could intercept a SelectEvent to revert it's effect.
    Sadly, there does not seem to be any SelectEvent.

    I found "textField.doubleClickSelectsWord" and "textField.tripleClickSelectsLine".
    It feels like mismanagement that is there this stuff, which is relevant only for advanced TextField users,
    while there is nothing like "textField.selectAllOnFocus", which would affect every TextField user and every TextField.

    Furthermore, we could implement a "SelectAll on focus" by using the FocusEvent. But we cannot undo the SelectAll as it is now.
    Thus, I argue that no SelectAll on focus should be default in Unity.
     
    Last edited: Mar 6, 2022
    ChGuidi and Nexer8 like this.
  6. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    440
    Hi @achimmihca and @vejab!

    We made some significant improvements to the TextField in 2022.1, you can learn more about this refactor from this post. Among other things, we added APIs to better control the selection such as selectAllOnFocus and selectAllOnMouseUp.
     
    achimmihca and Nexer8 like this.
  7. jciesla44

    jciesla44

    Joined:
    Dec 1, 2018
    Posts:
    4
    I'm using 2022.2 and selectAllOnFocus seems to do nothing at all. I've tried setting it to false in the UXML, in the start() function of the owning script, and in the function in which the textfield gains focus and every time I focus the textfield, everything is selected anyway just as it is without setting this bool. What am I missing?
     
  8. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    440
    Hi @jciesla44! I believe it has only been added as a UXML attribute in 2023.1. You'll have to do it through scripting. You may also want to use selectAllOnMouseUp.

    Code (CSharp):
    1. textField.selectAllOnFocus = false;
    2. textField.selectAllOnMouseUp = false
    3.  
     
    jciesla44 and dlorre like this.
  9. jciesla44

    jciesla44

    Joined:
    Dec 1, 2018
    Posts:
    4
    So, does this need to be on a subcomponent? Or just the root textfield? Because this is what I have and nonetheless, when it gains focus (by code), it selects all:

    Code (CSharp):
    1.  
    2. m_ChatInput = m_Root.Q<TextField>("ChatInput");
    3. m_ChatInput.selectAllOnFocus = false;
    4. m_ChatInput.selectAllOnMouseUp = false;
    5.  
    Does this selectAllOnFocus = false not apply to the textfield gaining focus via textField.Focus() because that's how I'm trying to do this and what I'm having issues with.

    FWIW, this is the code used to add text to the textfield then give it focus which is ignoring the selectAllOnFocus bit and, well, selecting all the new text:

    Code (CSharp):
    1.  
    2. m_ChatInput.value = "test";
    3. m_ChatInput.Focus();
    4. m_ChatInput.cursorIndex += m_ChatInput.value.Length;
    5.  
     
  10. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    440
    In this snippet, it's likely the selectIndex stays at the initial position. The difference between the selectIndex and the cursorIndex will be highlighted. Try:

    Code (csharp):
    1. m_ChatInput.cursorIndex = m_ChatInput.selectIndex = m_ChatInput.value.Length;
     
    jciesla44 likes this.
  11. jciesla44

    jciesla44

    Joined:
    Dec 1, 2018
    Posts:
    4
    Amazing, thank you so much. I appreciate your help - it works as advertised!
     
    HugoBD-Unity likes this.
  12. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    127
    It sucks that this feature wont be backported to at least 2021, the amount of times I lost work due to accidentally starting to type before I click the second time...
     
    zim22 likes this.
  13. zim22

    zim22

    Joined:
    Feb 25, 2018
    Posts:
    44
    please backport it to 2021 LTS
     
  14. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    440
    I understand the disappointment, but unfortunately, the changes are too big to backport to 2021.3. Have a look at this post for more information about the specific changes on the TextField in 2022.3.