Key.getAscii( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Key.getAscii( ) Method Flash 5

returns the ASCII value of the last key pressed
Key.getAscii()

Returns

An integer representing the ASCII value of the last key pressed.

Description

The getAscii( ) method returns the ASCII value of the last key that was pressed (note that it does not accept any arguments). Since not all keys have an ASCII value, getAscii( ) is normally used for detecting letters and numbers from the Latin 1 character set (used for Western European languages). Unlike getCode( ), getAscii( ) distinguishes between upper- and lowercase letters. But unlike getCode( ), it cannot differentiate between two keys with the same ASCII value, such as the 8 key on the main keyboard and the 8 key on the numeric keypad.

To convert an ASCII value to a string character, use String.fromCharCode( ), as shown in the following example.

To detect the pressing of specific keys relative to their physical location on a keyboard rather than their ASCII value (as would be desirable when using four keys in a diamond pattern to control game play), use getCode( ). Both getAscii( ) and getCode( ) are typically used within an onKeyDown( ) event listener, so that they are called once and only once per keypress.

Example

The following example shows how to distinguish between uppercase A and lowercase a.

if (Key.getAscii() =  = 65) {   trace("Uppercase A was pressed last."); } else if (Key.getAscii() =  = 97) {   trace("Lowercase a was pressed last."); } else {   trace("Some other key was pressed last."); }

Note that using getCode( ) in this example would not allow you to distinguish between uppercase A and lowercase a, as both use keycode 65. To rewrite it with getCode( ), you'd have to check the state of the Shift key, with isDown( ), or the Caps Lock key, with isToggled( ).

The following example demonstrates keystroke detection for a simple hangman word game. It uses an onKeyDown( ) event listener to detect a keypress and adds that key to a list of user guesses. Notice that keyListener is a generic object, demonstrating that any object defining an onKeyDown( ) or onKeyUp( ) callback handler can act as a listener. Objects without such callback handlers don't get notified of keyboard events, even if the objects are added to the event listener list with Key.addListener( ).

keyListener = new Object(); userGuesses = new Array(); guessNum = 0; keyListener.onKeyDown = function () {   var lastKey = Key.getAscii();   userGuesses[guessNum] = String.fromCharCode(lastKey);   guessNum++;   trace(userGuesses); } Key.addListener(keyListener);

See Also

Key.getCode( ), Key.isDown( ); Appendix B



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net