Insert text at cursor position in textInput - actionscript-3

i need to add text at cursor position on key_up event of keyboard.
first i move cursor using arrow.
then when i was trying to write anything it goes to end of the textInput.
because i used setrange().
Here is my code sample.....
var view:MainView;
eventMap.mapListener(view.numDisplay,KeyboardEvent.KEY_DOWN,onKeyBoardKeyDown);
private function onKeyBoardKeyDown(vEvent:KeyboardEvent):void
{
view.numDisplay.selectRange(view.numDisplay.text.length, view.numDisplay.text.length);
var cno:String=String.fromCharCode(vEvent.charCode);
var no:String=view.numDisplay.text;
if(no=="0") no="";
view.numDisplay.text=no+cno;
}
example:-
if i write
hii alll
then its ok.
after that i use arrow key to move cursor.
let's say my cursor was at
a
then if write any thing then it appears like
hii alll123
But i need output as
hii a123lll

i don't know why you are using the line:
view.numDisplay.selectRange(view.numDisplay.text.length, view.numDisplay.text.length);
but maybe you can change it to
view.numDisplay.selectRange(view.numDisplay.caretIndex, view.numDisplay.caretIndex);
to set the selection to the actual cursor position so your text won't be at the end but at the correct position.
Did you try without the line ? what problem do you try to correct with this ?
With the standard behavior, the cursor is always after the last character typed. I don't understand why you want to force it to be at the end of the text.

Related

How do I insert at a specific position in a Doc? (Inside a text item?)

My goal is to find the position of some text in a document, delete that text, and put something else its place. I thought I would start by just inserting the new text first, but it doesn't go anywhere near where I thought it would.
The relevant lines of code are here:
matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();
theDoc.getBody().insertParagraph(matchPosition, "The new stuff");
"put stuff here" is in the middle of a document with all sort of other text and formatting.
I'm guessing this can't be done the way I think it can. Can anyone point me in the right direction? Thanks in advance for your help.
The function you tried using, insertParagraph takes an index as parameter, which is the index of the paragraph, not the place when the text will go.
Instead of appending the text then removing what was there, you could simply replace it
theDoc.getBody().replaceText("put stuff here", "The new stuff");
Or, if you want to place text at a precise position, you could use the function editAsText of the body, and then insertText
matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();
theDoc.getBody().editAsText().insertText(matchPosition, "The new stuff");
If you want more info, you should check out the complete list of function of the class body here

Receiving click events from separate lines with AS3

I want to receive separate click events from separate lines in a text field, and every time a certain line is clicked by the user, I would like to highlight it and have an event happen.
I would ideally like this to happen with dynamic text, and not have to break the text apart by hand. Using the htmlText property is an option, but I am unsure as to how to bind clickEvents to separate elements.
Where do I begin?
There is no ready to use solution for this. But you can make it yourself using a few things:
set CLICK listener for the whole text field
listen for click and check the caretIndex property
use getLineIndexOfChar to check what's the line of the current caret position
use getLineOffset and getLineLength to get the position of the first and last character of that line
use setSelection to highlight this line
There might be some faster and easier way, but this is what works for sure :)
EDIT: decided to post the solution code, as I was wondering how exactly it works.. and it would be a shame to just leave it unpublished and make you do it instead :)
field.addEventListener(MouseEvent.CLICK, onTfClicked);
function onTfClicked(e:MouseEvent):void {
trace (field.caretIndex);
var line:uint = field.getLineIndexOfChar(field.caretIndex);
var start:uint = field.getLineOffset(line);
var end:uint = start + field.getLineLength(line);
field.setSelection(start, end);
}

How to access textbox's select function in Autocompletebox

I am trying to place the cursor at the end of the text in textbox inside Microsoft.Phone.Controls.AutoCompleteBox.
how can i do this?
It is strange that AutoCompleteBox has Focus function but no function for Select.
It's pretty much explained on this blog
Basically, it comes down to getting the inner TextBox
var textbox = GetTemplateChild("Text") as TextBox;
And then from there you can easily do whatever you would otherwise do with a TextBox.

Actionscript 3 - get position of Cursor in TextArea?

I am making something in Actionscript 3, where people can modify a piece of text, within a TextArea.
Now, it's easy to get the typed character, but using event.getChar.
But, I would also like to know where the character was typed: the (text)cursor position.
I've read about that it's easy to do with a TextField, however, I want to use a TextArea for a few reasons:
I've read about it being possible with a TextField, but I'm not sure how I would make that into an input field...
Also, the TextArea is recommended for multiline text.
If I could hack a TextField to behave like a TextArea, I'm fine with that.
So, my question:
How can I get the position of the cursor in a TextArea?
or
How can I make a TextField behave like a TextArea?
EDIT: I managed to make an input TextField, but the caretIndex returns xyz coordinates, pretty useless for text editing/comparing... Any suggestions on that?
you can see which letter was clicked in a textfield by doing the following:
var tf:TextField;
var clicked_on_index:int = tf.getCharIndexAtPoint(tf.mouseX, tf.mouseY);//find index of char clicked on in string
var clicked_on_char:String = tf.text.substr( clicked_on_index, 1 );//find char clicked on from textfield
or if you just want to know the position of the last character entered:
var tf:TextField;
tf.addEventListener(Event.CHANGE,function(event:Event):void{
var newCharacterPosition:int=tf.caretIndex;
var totalCharacters:int=tf.text.length;
});

How do I edit a text field in Selenium 2?

I can type text into a field using WebElement.sendKeys() but editing doesn't work: I can neither move the cursor nor delete the last character that I typed with e.sendKeys( Keys.BACK_SPACE )
How do I modify the value of a text field in Selenium 2 (WebDriver)?
You can definitely do that by either of the two methods. I have tried and it works.
e.click() # Positions the cursor at the end of the string
e.sendKeys(Keys.BACK_SPACE )
Or you could simply clear the text, and start over again:
e.clear()
e.sendKeys("What you want to send")
I found this solution that seems to work pretty well. It basically clicks on the text field WebElement, then sends Ctrl-End to put the cursor at the end of the text. Then sends the string that I had previously initialized.
(quickReplyTextArea is a text field WebElement that I have previous found, as is postQuickReplyButton (button instead of text field, obviously). replyText is a String that I initialized earlier)
quickReplyTextArea.click();
quickReplyTextArea.sendKeys(Keys.chord(Keys.CONTROL, Keys.END));
quickReplyTextArea.sendKeys(replyText);
postQuickReplyButton.click();
You can try clicking first into that text box, and use sendKeys() afterwards.