TextField.onKillFocus( ) Event Handler | Flash 6 |
callback invoked when the field loses focus |
The TextField, MovieClip, or Button object that now has focus, or null if no object received focus.
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( ).
Selection.onSetFocus( ), Selection.setFocus( ), TextField.onSetFocus( ), TextField.selectable, TextField.tabEnabled, TextField.tabIndex, TextField.type; Chapter 10