Name

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

Availability

Flash 5

Synopsis

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. Since not all keys have an ASCII value, getAscii( ) is normally used for detecting letters and numbers from the Latin 1 character set (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 detect the pressing of specific keys relative to their physical location on a keyboard rather than their ASCII value (such as would be desirable when using four keys in a diamond pattern to control game play), use getCode( ).

Example

The following example demonstrates keystroke detection for a simple hangman word game. It uses a keyDown event handler to identify the pressing of a key and adds that key to a list of user guesses:

onClipEvent (keyDown) {
  var lastKey = Key.getAscii( );
  guessNum++;
  userGuesses[guessNum] = String.fromCharCode(lastKey);
}

See Also

Key.getCode( ); Appendix B, and Section 10.11.4 in Chapter 10

Get ActionScript: The Definitive Guide 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.