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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Keyboard input<br />

tf.border = true;<br />

tf.type = "input";<br />

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

tf.addEv<strong>en</strong>tList<strong>en</strong>er(KeyboardEv<strong>en</strong>t.KEY_UP,reportKeyUp);<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) + " (key code: " +<br />

ev<strong>en</strong>t.keyCode + " character code: " + ev<strong>en</strong>t.charCode + ")");<br />

if (ev<strong>en</strong>t.keyCode == Keyboard.SHIFT) tf.borderColor = 0xFF0000;<br />

}<br />

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

{<br />

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

ev<strong>en</strong>t.keyCode + " character code: " + ev<strong>en</strong>t.charCode + ")");<br />

if (ev<strong>en</strong>t.keyCode == Keyboard.SHIFT)<br />

{<br />

tf.borderColor = 0x000000;<br />

}<br />

}<br />

The TextField class also reports a textInput ev<strong>en</strong>t that you can list<strong>en</strong> for wh<strong>en</strong> a user <strong>en</strong>ters text. For more<br />

information, see “Capturing text input” on page 378.<br />

Note: In the AIR runtime, a keyboard ev<strong>en</strong>t can be canceled. In the Flash Player runtime, a keyboard ev<strong>en</strong>t cannot be<br />

canceled.<br />

Key codes and character codes<br />

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

You can access the keyCode and charCode properties of a keyboard ev<strong>en</strong>t to determine what key was pressed and th<strong>en</strong><br />

trigger other actions. The keyCode property is a numeric value that corresponds to the value of a key on the keyboard.<br />

The charCode property is the numeric value of that key in the curr<strong>en</strong>t character set. (The default character set is UTF-<br />

8, which supports ASCII.)<br />

The primary differ<strong>en</strong>ce betwe<strong>en</strong> the key code and character values is that a key code value repres<strong>en</strong>ts a particular key<br />

on the keyboard (the 1 on a keypad is differ<strong>en</strong>t than the 1 in the top row, but the key that g<strong>en</strong>erates “1” and the key<br />

that g<strong>en</strong>erates “!” are the same key) and the character value repres<strong>en</strong>ts a particular character (the R and r characters<br />

are differ<strong>en</strong>t).<br />

Note: For the mappings betwe<strong>en</strong> keys and their character code values in ASCII, see the flash.ui.Keyboard class in the<br />

ActionScript 3.0 Refer<strong>en</strong>ce for the Adobe Flash Platform.<br />

The mappings betwe<strong>en</strong> keys and their key codes is dep<strong>en</strong>d<strong>en</strong>t on the device and the operating system. For this reason,<br />

you should not use key mappings to trigger actions. Instead, you should use the predefined constant values provided<br />

by the Keyboard class to refer<strong>en</strong>ce the appropriate keyCode properties. For example, instead of using the key mapping<br />

for the Shift key, use the Keyboard.SHIFT constant (as shown in the preceding code sample).<br />

Last updated 6/6/2012<br />

560

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

Saved successfully!

Ooh no, something went wrong!