After adding text to an input field, I want focus to set the caret to the end
I have an input field that I want to automatically fill in parts of based on the user's clicking on a set of toggles. I then want to set the focus to that input field and have the cursor at the end of the automatically filled in text so the player can add personalized text.
I currently have: myInputField.text = "text I want to add:" myInputField.Select();
This results in the input field hilighting the text I added so if the player just types away it will overwrite the automatic text.
How do I get the new text NOT hilighted and the cursor at the end of the text. It seems like there should be a "Get end of text line cursor position" or something but I haven't found it and all I can find is how to align the cursor with the pivot so it lines up correctly vertically.
Answer by neomarian · Nov 28, 2019 at 05:58 PM
yield return new WaitForEndOfFrame();
InputField.caretPosition = InputField.text.Length;
InputField.ForceLabelUpdate();
OR
yield return new WaitForEndOfFrame();
InputField.MoveTextEnd(true);
put this in an IEnumerator function or remove the yield statement and call this in LateUpdate()
Brilliant, this works exactly as intended! Only issue is the text selection flash, but $$anonymous$$or detail, and can be hidden with an alpha 0 text selection color. Thanks!
Answer by SadSwede · Mar 06, 2018 at 05:12 PM
Just incase people come across here like i did, here is the solution:
inputField.caretPosition = inputField.text.Length;
I just tried it in my code, it didn't work. $$anonymous$$aybe there is some correction, can you repost after taking a look?
Answer by Zogg · Nov 13, 2019 at 02:30 PM
For TextMeshPro input fields, see https://forum.unity.com/threads/move-cursor-to-end-of-text.530903/#post-3525370
Answer by JWLewis777 · Dec 10, 2017 at 08:11 PM
Even though this is an old post, I am reviving it because this is still an issue.
I have been going through this for hours now, trying everything I can find.
If you use any kind of code to set focus on the inputbox the existing text will get highlighted.
I had to turn the highlight alpha to 0 to hide it, then position the cursor to the end of the text in the update method.
In the end, the cursor would jump to the middle of the inputbox even though the cursor position number was showing the correct number in the editor. I finally gave up on the inputfield as a horribly bugged POS.
I am now working on making my own inputfield for the same reason I had to create my own custom virtual keyboard, buggggggssss!!!
:(
Answer by ManjuYadav · Mar 30 at 11:25 AM
In TextMeshPro - Inputfield ---> Control settings --> OnFocus Select All (should be disabled). This is available only in TextMeshPro.
Your answer

Follow this Question
Related Questions
Handling Multiple U.I. Input Fields 0 Answers
Mousing over input field highlights all parent objects if they have a button component 0 Answers
Is it possible to input Graphic images in inputfield instead of text? 0 Answers
How to check what UI type is currently in focus 1 Answer
Save multiple inputs for one item and print out all items 0 Answers