Name

Key.isDown( ) Method — check whether a specific key is currently depressed

Availability

Flash 5

Synopsis

Key.isDown(keycode)

Arguments

keycode

A number representing the keycode of the key to check. May also be one of the Key constants (e.g., Key.UP, Key.BACKSPACE).

Returns

A Boolean indicating whether the key specified by keycode is pressed (true) or not pressed (false).

Description

The isDown( ) method tells us whether the key specified by keycode is currently being pressed. It offers arbitrary, immediate access to the state of the keyboard and is best used with systems that require constant key-based input or that detect the pressing of simultaneous keys.

One important advantage of isDown( ) over getCode( ) and getAscii( ) is its ability to detect the simultaneous pressing of multiple keys. By checking for both Key.UP and Key.RIGHT, for example, we may determine that a spaceship in a game should be moved diagonally. Depending on the placement of the specific keys being tested, the maximum number of keys that can be simultaneously detected may be as low as three.

Example

The isDown( ) method is normally used to create systems that undergo a constant update with each passing frame. In the following code, we rotate and thrust a spaceship on any frame where the appropriate arrow keys are being pressed. Note that if you need to detect two keys simultaneously, you should use separate if statements. In this example, the state of the right arrow key is ignored if the left arrow key is also being ...

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.