TextField.onKillFocus( ) Event Handler

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
TextField.onKillFocus( ) Event Handler Flash 6

callback invoked when the field loses focus
theField.onKillFocus(newFocus)

Arguments

newFocus

The TextField, MovieClip, or Button object that now has focus, or null if no object received focus.

Description

The onKillFocus( ) event is triggered when theField loses focus. A text field is said to have focus when it contains the text insertion point or when characters within it are selected. A field gains focus when:

  • The user clicks it with the mouse or navigates to it with the Tab key.

  • Selection.setFocus( ) assigns focus programmatically.

A field loses focus (and onKillFocus( ) is executed) when:

  • The user clicks somewhere outside the field or navigates away from it via the Tab key, at which point focus may be transferred to whatever was tabbed to (a text field, movie clip, or button). Clicking a button or movie clip does not give the button or clip focus. If focus was transferred, the newly focused object is accessible within onKillFocus( ) via the newFocus parameter; otherwise, newFocus is null.

  • Selection.setFocus( ) assigns focus to another object programmatically. The newly focused object is accessible within onKillFocus( ) via newFocus.

When a text field's selectable property is false, the field cannot be focused by the user but can be focused programmatically with Selection.setFocus( ).

The following code creates a callback function that responds to onKillFocus( ). Run it in Test Movie mode and try clicking on and off the text field. The Output window displays a message when the event is triggered.

// Create a text field that accepts user input this.createTextField("theField_txt", 1, 0, 0, 200, 20); theField_txt.type = "input"; theField_txt.border = true; // Assign the onKillFocus() callback using a function literal theField_txt.onKillFocus = function (newFocus) {   trace(this + " lost focus. Focus is now " + newFocus); };

Notice that from the body of the callback we refer to theField_txt using the this keyword. To stop a callback from handling the onKillFocus( ) event, use the delete operator, as in:

// Make sure to omit the () operator after the function name! delete theField_txt.onKillFocus;

The onKillFocus( ) event can be used to trigger the validation of text field input when the user has finished entering text. Or, in combination with onSetFocus( ), it can bring attention to a focused text field with color (see the Example under TextField.onSetFocus( )). To capture all focus events centrally, rather than for a single instance, use Selection.onSetFocus( ).

See Also

Selection.onSetFocus( ), Selection.setFocus( ), TextField.onSetFocus( ), TextField.selectable, TextField.tabEnabled, TextField.tabIndex, TextField.type; Chapter 10



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net