13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 30: Keyboard input<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Your application can capture and respond to keyboard input and can manipulate an IME to let users type non-ASCII<br />

text characters in multibyte languages. Note that this section assumes that you are already familiar with the<br />

ActionScript 3.0 ev<strong>en</strong>t model. For more information, see “Handling ev<strong>en</strong>ts” on page 125.<br />

For information on discovering what kind of keyboard support is available (such as physical, virtual, alphanumeric, or<br />

12-button numeric) during runtime, see “Discovering input types” on page 557.<br />

An Input Method Editor (IME) allows users to type complex characters and symbols using a standard keyboard. You<br />

can use the IME classes to <strong>en</strong>able users to take advantage of their system IME in your applications.<br />

More Help topics<br />

flash.ev<strong>en</strong>ts.KeyboardEv<strong>en</strong>t<br />

flash.system.IME<br />

Capturing keyboard input<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Display objects that inherit their interaction model from the InteractiveObject class can respond to keyboard ev<strong>en</strong>ts<br />

by using ev<strong>en</strong>t list<strong>en</strong>ers. For example, you can place an ev<strong>en</strong>t list<strong>en</strong>er on the Stage to list<strong>en</strong> for and respond to keyboard<br />

input. In the following code, an ev<strong>en</strong>t list<strong>en</strong>er captures a key press, and the key name and key code properties are<br />

displayed:<br />

function reportKeyDown(ev<strong>en</strong>t:KeyboardEv<strong>en</strong>t):void<br />

{<br />

trace("Key Pressed: " + String.fromCharCode(ev<strong>en</strong>t.charCode) + " (character code: " +<br />

ev<strong>en</strong>t.charCode + ")");<br />

}<br />

stage.addEv<strong>en</strong>tList<strong>en</strong>er(KeyboardEv<strong>en</strong>t.KEY_DOWN, reportKeyDown);<br />

Some keys, such as the Ctrl key, g<strong>en</strong>erate ev<strong>en</strong>ts ev<strong>en</strong> though they have no glyph repres<strong>en</strong>tation.<br />

In the previous code example, the keyboard ev<strong>en</strong>t list<strong>en</strong>er captured keyboard input for the <strong>en</strong>tire Stage. You can also<br />

write an ev<strong>en</strong>t list<strong>en</strong>er for a specific display object on the Stage; this ev<strong>en</strong>t list<strong>en</strong>er is triggered wh<strong>en</strong> the object has the<br />

focus.<br />

In the following example, keystrokes are reflected in the Output panel only wh<strong>en</strong> the user types inside the TextField<br />

instance. Holding the Shift key down temporarily changes the border color of the TextField to red.<br />

This code assumes there is a TextField instance named tf on the Stage.<br />

Last updated 6/6/2012<br />

559

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!