Table of Contents

Selection.getCaretIndex( ) Method Flash 5

retrieve the index of the insertion point in a text field
Selection.getCaretIndex()

Returns

The index of the insertion point in the currently focused text field. If no text field has keyboard focus, it returns -1. If the text field with focus is empty, it returns 0.

Description

The getCaretIndex( ) method indicates the insertion point (i.e., the location of the cursor) in a text field. The cursor appears as an I-beam when a text field has keyboard focus. Use setSelection( ) to set the location of the insertion point.

Example

Because getCaretIndex( ) returns -1 when no text field has focus, we can determine whether any field has focus by checking whether getCaretIndex( ) is equal to -1

The following example checks whether any text field has keyboard focus:

if (Selection.getCaretIndex() =  = -1) {
  trace("No field has focus");
}

Reader Exercise: Rewrite the preceding example as a Boolean function, isTextFieldActive( ), which returns true if a text field has focus and returns false otherwise. Modify it to accept an optional text field identifier and check whether that specific text field is active. (Hint: See Selection.getFocus( )).

See Also

Selection.getFocus( ), Selection.setSelection( )


Table of Contents