Assuming you're using the Flex TextInput component, call the setSelection() method with the same value for the begin and end parameters. That moves the insertion point to the specified location.
that works great. Now i can see the cursor move as i type buttons in my custom keyboard. Thanks for the tip.
But one thing I can't figure out yet is that: if the user directly enters into the TextInput field and moves the curson to somewhere in between,
at this point when I press the buttons in my custom keyboard, how do i get new characters to be entered at the current cursor position (not at the end of the already typed-in string).
Basically, the user can either enter into the textinput directly or use my own custom keyboard.
The scenario I am talking about is: i want to create my own dialer for example. I have attached something similar, as skype dialer keypad.
Picture 12.png 22.9 K
In that case you need to programmatically insert the text at the selection point. You get the selection point using the selectionBeginIndex and selectionEndIndex properties. I have an app where I do that, using this code:
// textInput is the name of the TextInput control
private function insertTextAtSelection(text:String):void
var start:int = textInput.selectionBeginIndex;
var end:int = textInput.selectionEndIndex;
var hasSelection:Boolean = start != end;
textInput.text = textInput.text.substring(0, start) + text + textInput.text.substring(end);
textInput.selectionBeginIndex = start;
textInput.selectionEndIndex = start + text.length;
textInput.selectionBeginIndex = textInput.selectionEndIndex = end + text.length;
This is what the code does:
- gets the starting and ending positions of the insertion point (in case the user has an active selection rather than just an insertion point).
- concatenates the old text with the new text and writes it into the text input
- restores the user's previous selection or insertion point (In this case I'm setting the selectionBeginIndex and selectionEndIndex properties rather than calling setSelection(), but the end result is the same.)
Your code and input was really helpful. It works.