x


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!

more ▼

asked Jul 20, 2010 at 07:43 AM

Trent gravatar image

Trent
58 10 7 10

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Jul 20, 2010 at 09:03 AM

qJake gravatar image

qJake
12.7k 92 136 214

(comments are locked)
10|3000 characters needed characters left

TextEditor te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (te != null) { te.MoveCursorToPosition(new Vector2(5555, 5555)); }

more ▼

answered May 10, 2011 at 12:31 PM

valdemar gravatar image

valdemar
46 1

(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Dec 02, 2013 at 05:56 PM

lcairco gravatar image

lcairco
96 1 1 3

(comments are locked)
10|3000 characters needed characters left

how do you get the current position of the cursor?

Thanks

more ▼

answered Sep 10, 2010 at 02:52 PM

Bill 2 gravatar image

Bill 2
189 34 25 35

(comments are locked)
10|3000 characters needed characters left

So Unity3D 3 is out since a while. Did something change regarding this problem? I've examined the whole scripting-reference but unfortunately found nothing.

more ▼

answered Apr 06, 2011 at 07:22 AM

Asse gravatar image

Asse
168 23 23 28

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x322
x293
x249
x131

asked: Jul 20, 2010 at 07:43 AM

Seen: 10799 times

Last Updated: Dec 02, 2013 at 05:56 PM