Filtering Keyboard Input

In the default case, any characters typed by the user will appear in an editable field. You can intercept keyboard input, before it appears in the field, with a keyDown handler attached to the field sprite. Movie-wide key events can also be trapped with the keyDownScript. Refer to the previous examples.

Tip

Keyboard events not trapped by a field’s sprite or cast script are passed to the frame script or movie scripts. If you attach a keyDown handler to a sprite, you must include the pass command for the field cast member to actually receive the keyboard character(s).

It is possible to attach multiple keyDown handlers to a single sprite. Table 10-6 shows what happens when keyDown events are passed or consumed explicitly or implicitly. See Table 10-1 for the event passing order.

Table 10-6. KeyDown Event Passing

Command Used in First Behavior

Passed to Other Behaviors?

Passed to Editable Field?

pass

Yes

Only if all Behaviors issue pass

<None specified> or dontPassEvent

Yes

No, but later Behaviors can manually append character to field

stopEvent

No

No

Don’t forget to trap undesirable keys when requesting user input. Example 10-7 shows how to filter out certain keys and perform some action when the Return or Enter key is pressed. This keyDown handler attached to a field sprite prevents the user from entering any spaces.

Example 10-7. Disallowing Characters

on keyDown me
 if the key = RETURN or the key = ENTER then
-- Process the user entry set contents ...

Get Lingo in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.