Handling button events

You can use buttons to quickly add interactivity to your Flash Lite applications. Flash Lite supports the same button events as Flash Player on desktop computers, although some events (for example, onDragOut) are only available on devices that have a mouse or stylus interface. On devices that have a keypad interface only, a button must have keypad focus before it will generate any events.

Flash Lite supports the following ActionScript button events:

Button event

Description

onDragOut

Supported only on devices that have a mouse or stylus. Invoked when the user presses the mouse button over the button and the pointer is then dragged outside of the button.

onDragOver

Supported only on devices that have a mouse or stylus. Invoked when the user presses and drags the mouse button outside and then over the button.

onKeyDown

Invoked when the button has focus and a key is released.

onKeyUp

Invoked when the button has focus and a key is pressed.

onKillFocus

Invoked when focus is removed from a button.

onPress

Invoked when the user presses the select key on the device when the button has focus.

onRelease

Invoked when the user releases the select key on the device when the button has focus.

onReleaseOutside

Invoked when the mouse button is released while the pointer is outside the button after the button is pressed while the pointer is inside the button.

onRollOut

Invoked when a button loses focus.

onRollOver

Invoked when a button receives focus.

onSetFocus

Invoked when a button has input focus and a key is released.

The following procedure demonstrates how to create a simple application that handles button events. For an example of using buttons to create a menu, see Creating a simple menu using buttons and default navigation.

To create a button event handler:

  1. Create a new document from the Flash Lite 2.0 template that you created in Creating a Flash Lite document template in Getting Started with Flash Lite 2.x, and save it as custom_focus_manager.fla.
  2. Select Window > Common Libraries > Buttons to open an external library of prebuilt button symbols.
  3. In the Library panel, double-click the classic buttons folder to open it, and then open the Circle Buttons folder.
  4. Drag an instance of the Menu button symbol to the Stage.
  5. In the Property inspector, in the Instance Name text box, type btn_1.
  6. Drag another instance of the same button to the Stage and position it directly below the first button.
  7. In the Property inspector, in the Instance Name text box, type btn_2.
  8. In the Timeline, select Frame 1 in the layer named ActionScript.
  9. Open the Actions panel (Window > Actions) and enter the following code:
    // Disable the focus rectangle because buttons have an over state
    _focusRect = false;
    
    // Event handlers for btn_1
    btn_1.onPress = function() {
        trace("You pressed Button 1");
    }
    btn_1.onRelease = function() {
        trace("You released Button 1");
    }
    btn_1.onRollOver = function() {
        trace("Button 1 has focus");
    }
    btn_1.onRollOut = function() {
        trace("Button 1 lost focus");
    }
    
    // Event handlers for btn_2
    btn_2.onPress = function() {
        trace("You pressed Button 2");
    }
    btn_2.onRelease = function() {
        trace("You released Button 2");
    }
    btn_2.onRollOver = function() {
        trace("Button 2 has focus");
    }
    btn_2.onRollOut = function() {
        trace("Button 2 lost focus");
    }
    
  10. Test the application in the emulator (Control > Test Movie).

    Watch the messages in the Output panel as you press the up and down arrow keys on the emulator's keypad.



Other types of objects support different events; for example, the TextField object includes an onChanged event that is invoked when the content of a text field changes. You can write event handler code for these events using the same format as the button event handlers in this procedure. For more information about the events supported for text fields and movie clips, see the TextField and MovieClip entries in the Flash Lite 2.x ActionScript Language Reference.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00004669.html