# Handling Events

An event in a Flash movie is the result of something happening. For example, an event can occur as the result of a user action, like clicking the mouse, or pressing a key on the keyboard. In either case, we say an event was generated when the user clicks the mouse or presses the key. These types of events (mouse clicks, key presses) are commonly called user events, since they occur as a result of direct user interaction.

Other types of events are generated automatically by Flash Player, like when a movie clip first appears on the Stage, or an external file has finished loading. These types of events are called system events, since they aren't generated by the user directly but by the system, or Flash Player.

In either case, the event itself doesn't actually "do" anything or cause anything to happen. Your job, as the programmer, is react to, or handle, the event in some way. The event lets you know when something has happened; it's up to decide what to do in response to that event. For example, when a user clicks a button on the Stage you might want to advance the playback head to the next frame, or load a new Flash movie over the network. Similarly, when an XML file finishes loading over the network you'll want to parse the contents of that filewant to load the contents of the file into a text field.

This chapter discusses the different types of events that can occur in a Flash movie, when they occur, and how you can handle them.

Actions attached to a button or movie clip are enclosed in special actions called handlers. The `onClipEvent` and `on` actions are called handlers because they "handle" or manage an event. You can specify one or more events for each handler. Movie clip and button actions execute when the event specified by the handler occurs. You can attach more than one handler to an object if you want different actions to execute when different events occur.

Several onClipEvent handlers attached to a movie clip on the Stage

The `onClipEvent` action handles movie clip events, and the `on` action handles button events. You can also use the `on` action with movie clips to create a button movie clip, a movie clip that receives button events.

Movie clip events and button events can also be handled by methods of the MovieClip and Button objects. You must define a function and assign it to the event handler method; the function executes when the event occurs. You can use event methods to handle events for dynamically created movie clips. Event methods are also useful for handling all events in a movie in one script: you don't have to attach the script to the object whose event you are handling.

For example, if you have a button on the Stage and you use the Actions panel to add a `trace` action, the following code appears:

    on (release) {
    	trace("You clicked me!");
    }

You could use a method to create the same effect, as in the following:

    myMovieClip.onRelease = function() {
    	trace("You clicked me!");
    }

For more information, see Chapter 14, "Working with Movie Clips and Buttons," in Using Flash.

The following table lists button event handlers and methods:

Event handler actions | Event handler methods
`on (press)` | `onPress`
`on (release)` | `onRelease`
`on (releaseOutside)` | `onReleaseOutside`
`on (rollOver)` | `onRollOver`
`on (rollOut)` | `onRollOut`
`on (dragOver)` | `onDragOver`
`on (dragOut)` | `onDragOut`
`on (keyPress"...")` | `onKeyDown`, `onKeyUp`

The following table lists movie clip event handlers and methods:

Event handler actions | Event handler methods
`onClipEvent (load)` | `onLoad`
`onClipEvent (unload)` | `onUnload`
`onClipEvent (enterFrame)` | `onEnterFrame`
`onClipEvent (mouseDown)` | `onMouseDown`
`onClipEvent (mouseUp)` | `onMouseUp`
`onClipEvent (mouseMove)` | `onMouseMove`
`onClipEvent (keyDown)` | `onKeyDown`
`onClipEvent (keyUp)` | `onKeyUp`
`onClipEvent (data)` | `onData`

ActionScript also allows you to handle events for text fields and other ActionScript objects. For more information, see the online ActionScript Dictionary in the Help menu.