Lingo Dictionary > G-K > on keyDown |  |

on keyDown

Syntax

    on keyDown
    	statement(s)
    end

Description

System message and event handler; contains statements that run when a key is pressed.

When a key is pressed, Director searches these locations, in order, for an `on keyDown` handler: primary event handler, editable field sprite script, field cast member script, frame script, and movie script. For sprites and cast members, `on` `keyDown` handlers work only for editable text and field members. A `keyDown` event on a different type of cast member, such as a bitmap, has no effect. (If pressing a key should have the same response throughout the movie, set `keyDownScript`.)

Director stops searching when it reaches the first location that has an `on keyDown` handler, unless the handler includes the `pass` command to explicitly pass the `keyDown` message on to the next location.

The `on keyDown` event handler is a good place to put Lingo that implements keyboard shortcuts or other interface features that you want to occur when the user presses keys.

The Director player for Java responds to `keyDown` messages only if the movie has focus in the browser. The user must click in the applet before the applet can receive any keys that the user types.

When the movie plays back as an applet, an `on keyDown` handler always traps key presses, even if the handler is empty. If the user is typing in an editable field, an `on keyDown` handler attached to the field must include the `pass` command for the key to appear in the field.

Where you place an `on keyDown` handler can affect when it runs.

| To apply the handler to a specific editable field sprite, put the handler in a sprite script.
| To apply the handler to an editable field cast member in general, put the handler in a cast member script.
| To apply the handler to an entire frame, put the handler in a frame script.
| To apply the handler throughout the entire movie, put the handler in a movie script.

You can override an `on keyDown` handler by placing an alternative `on keyDown` handler in a location that Lingo checks before it gets to the handler you want to override. For example, you can override an `on keyDown` handler assigned to a cast member by placing an `on keyDown` handler in a sprite script.

Example

This handler checks whether the Return key was pressed and if it was, sends the playback head to another frame:

    on keyDown
    	if the key = RETURN then go to frame "AddSum"
    end keyDown

See also

`charToNum()`, `keyDownScript`, `keyUpScript`, `key()`, `keyCode()`, `keyPressed()`