Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: TextField Focus

I'm trying to handle a focus event on a TextField so I can select all the text when focusing (tab or click). Seems like I'm doing something wrong here ?

txtTextField.addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);
function handleFocusIn() {
 //select all text here
}
like image 287
Yens Avatar asked Jan 21 '26 04:01

Yens


2 Answers

I needed the same thing, to select the contents of a textfield when it receives focus.

I tried:

A) Simply selecting after a FocusEvent. This doesn't seem to work (my guess is that FocusEvents are fired before the mouse click is being processed, which in turn will undo the selection).

B) Selecting on every mouse click. This works, but this is very annoying for a user who wants to select only a part of the text later, since this attempt will always result in -all- the content being selected.

The following workaround seems to work though:

    myTextField.addEventListener(MouseEvent.CLICK, selectAllOnce);

    function selectAllOnce(e:MouseEvent) {
        e.target.removeEventListener(MouseEvent.CLICK, selectAllOnce);
        e.target.addEventListener(FocusEvent.FOCUS_OUT, addSelectListener);
        selectAll(e);
    }

    function addSelectListener(e:FocusEvent) {
        e.target.addEventListener(MouseEvent.CLICK, selectAllOnce);
        e.target.removeEventListener(FocusEvent.FOCUS_OUT, addSelectListener);
    }

    function selectAll(e:Event) {
        e.target.setSelection(0, e.target.getLineLength(0));
    }

Hope that helps. I personally think it would be most logical if adobe simply added an option for this for the TextField object.

like image 98
Mattijs Avatar answered Jan 23 '26 03:01

Mattijs


Your handleFocusIn should have the signature

function handleFocusIn(event:FocusEvent) // or just Event
like image 33
Vinay Sajip Avatar answered Jan 23 '26 03:01

Vinay Sajip