Actionscript- double click to select a word in a textfield? - actionscript-3

Can someone tell me how I might enable double clicking to select a word in a input textfield?

It should do this by default if you are using a TextField with type TextFieldType.INPUT.

If Spencer's idea doesn't work you could do the following:
Set doubleClickEnabled=true on the TextField.
Add a listener for the DOUBLE_CLICK.
When the listener fires, convert the event's (x,y) to a character index by using TextField.getCharIndexAtPoint.
Search before and after that point for either a space or string end/begin and use those two values to set the selection index.

Related

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);
}

Insert text at cursor position in textInput

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.

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.

Input Textfield date mask

I need to restrict my users to input only dates with a custom format. I want to have something like this example in JQuery: http://www.youtube.com/watch?v=BMaTiGKykl8&feature=player_embedded
How can i archieved this in AS3?
You'll have to catch the KeyPressed event on the TextField. Check if the key pressed is OK at the specified position, if yes, do nothing, if not, cancel the event (Event.cancel = true).
Alternatively (for more features, like these auto added slashes) you can ALWAYS cancel the Event, and use the TextField's selection together with a String and some checking to always make a new version of the TextField's text.
Why not use a DateField and format the data as you see fit?
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/DateField.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2

TextField() - filter backspace key away with event listener

How to prevent a specific key such as backspace key functioning from TextField editable field, preventDefault does not seem to work:
public function handleEvents(evt:KeyboardEvent):void {
if (evt.type == KeyboardEvent.KEY_UP) {
if (evt.keyCode==8){
evt.preventDefault () ;
}
}
I recommend listening for the KEY_DOWN event if anything, but likely that won't work either. IIRC these type of events are a bit special and you can't really stop them.
What I suspect you need to do is to store a copy of the text and whenever you detect a change you don't like just set it back to your stored version.
What I have done is set focus to another temporary (and off-stage) text field and in my keydown handler return focus to the my text field for the keys I want to get through. Setting focus just for keys you want to filter out also works.
Try adding evt.stopImmediatePropagation()