Start the Flash program. Click "New" from the File menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the New Document dialog box that appears. Click the "OK" button to close the dialog box.
Select the "Actions" option from the Window menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button and type in the following code to add a keyboard event listener to the stage that will instruct Flash to process the instructions within the "pressedKey" function when a key has been pressed:
stage.addEventListener (KeyboardEvent.KEY_DOWN, pressedKey)
Type the following code, starting at the next line in the Actions editor, so that the function "pressedKey" will detect if the space bar key has been pressed and then instruct Flash to process the instructions that are within the function "KeyDetector":
function pressedKey(event: KeyboardEvent):void
{
if(event.keyCode == 32) {keyDetector()}
}
Type the following code, starting at the next line in the Actions editor, to display the message "The space bar key was pressed" after the space bar key has been pressed:
function keyDetector():void {
trace("The spacebar key was pressed")
};
Copy and paste the following code into the Actions editor to run the ActionScript space bar program error free:
stage.addEventListener (KeyboardEvent.KEY_DOWN, pressedKey)
function pressedKey(event: KeyboardEvent):void
{
if(event.keyCode == 32) {keyDetector()}
}
function keyDetector():void {
trace("The spacebar key was pressed")
};
Click the "TestMovie" option in the Control menu on the main Flash menu bar to play the ActionScript space bar movie. Position your mouse cursor over the Flash stage and press the space bar key on your keyboard. Verify that the message "The spacebar key was pressed" appears in the Output panel.