- Home /
Moving the keyboard cursor position to the end of a TextField
I'm working on a chat GUI at the moment and I stumbled across an interesting issue that I haven't been able to figure out. If I use am searching for a particular string and replacing with something else, the cursor stay at the previous location in the string. For example:
chatEntryValue = GUI.TextField(new Rect(y, x, w, h), chatEntryValue);
chatEntryValue = Regex.Replace(chatEntryValue ,"^/s ", "/Squad ");
The resulting cursor position will be (pipe as the cursor position) [/Sq|uad ]
I've tried using chatEntryValue.Remove(1,1) & chatEntryValue.Insert(1,"Squad ") with the same results and even gone so far as unfocusing off the TextField and containing window and refocusing.
Does anyone out there know of a way to move the cursor to the end of the TextField? I'd even settle for simulating Right arrow key presses on the keyboard.
Any help would be appreciated!
Look here, excellent example http://answers.unity3d.com/questions/210808/using-guifocuscontrol-on-textfield-selects-all-tex.html
Answer by qJake · Jul 20, 2010 at 09:03 AM
Looking around, unfortunately there is no way to move the text cursor around inside a Text Area or Textbox. You can get the current position of the cursor, but you cannot set it.
I would suggest this feature be added using the "Report bug" feature inside of Unity. There is an option there for suggesting new features.
And, perhaps, this functionality has been added in Unity 3, which is due out any time now. You could always wait and see if they've included it in there or not. As of right now, though, it looks like this isn't possible.
Answer by YasinJavaid_ · Mar 29, 2017 at 07:19 AM
late but someone may help. inputfeild.MoveTextEnd(false);
Answer by lcairco · Dec 02, 2013 at 05:56 PM
This works for me, where s is the string in the text box:
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
editor.selectPos = s.Length + 1;
editor.pos = s.Length + 1;
Answer by valdemar · May 10, 2011 at 12:31 PM
TextEditor te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (te != null) { te.MoveCursorToPosition(new Vector2(5555, 5555)); }
Answer by Bill 2 · Sep 10, 2010 at 02:52 PM
how do you get the current position of the cursor?
Thanks
Your answer
Follow this Question
Related Questions
Editable text field in Android with cursor 0 Answers
Textfield button PLZ HELP!!! 1 Answer
Use specific TouchScreenKeyboardType for different text fields 3 Answers
Need to Find Distance Between Mouse Cursor and Object in a 2.5D SideScroller 1 Answer
Disabling the inputfield above the keyboard or disable it all 0 Answers