Chapter 10. Keyboard Events
Keyboard Events
Whenever a standard (non-modifier) key is pressed or released, Director generates a keyDown
or keyUp
event. Only script attached to editable field sprites, frame scripts, and movie scripts receive keyboard events. All field sprites, even non-editable ones, can receive the standard sprite events (mouseUp
, mouseDown
, mouseEnter
, mouseLeave
, and so on) Refer to Chapter 7, for details on manipulating strings and text. See especially Example 7-7. Refer to Chapter 12, Text and Fields, in Director in a Nutshell for more details on working with fields.
Tip
Keyboard characters are sent automatically to an editable field sprite with keyboard focus, unless the keyDown
event is intercepted by an onkeyDown
handler in a sprite or castmember script first (or explicitly stopped in the keyDownScrip
t primary event handler).
The key being pressed “rides along” with the keyDown
event. If an editable field sprite receives the keyDown
event, the last key pressed will be added to the field. Non-editable fields, editable fields without keyboard focus, and non-field sprites do not receive keyDown
and keyUp
events. Rich text sprites can not be edited at run time. You must use field sprites for dynamic text.
Your keyDown
handler will be called even when special characters, such as the arrow keys, are pressed. Simply pass
these onto the field sprite.
Tip
Fields will automatically recognize the arrow keys and Delete key to perform rudimentary editing (see "Editing Keys
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.