Start the Flash program. Click "New" from the "File" menu on the main Flash menu bar. Click the "Flash File(ActionScript 3.0)" file type selection from the "New Document" dialog box that appears. Click the "OK" button on the lower right-hand side of this dialog box to close the dialog box and return to the Flash workspace.
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 left mouse button to position the cursor on the first line so you can type in ActionScript 3 code.
Type the code to declare a Boolean variable -- a variable that can only be set to a value of true or false -- named "leftArrowSelected." Type the ActionScript code statement "var leftArrowSelected: Boolean" on the first line in the "Actions" editor. Press the "Enter" key on your keyboard to move the mouse cursor to the second line in the "Actions" editor.
Type the code that will create a "KEY_DOWN" event listener that will detect if a key has been pressed. Name the function in the event listener code that the event listener calls as "keyDown_fn." Type this ActionScript event listener code as "stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_fn)" on the second line of the "Actions" editor. Press the "Enter" key on your keyboard to move the mouse cursor to the third line in the "Actions" editor.
Type the code listed below in the "Actions" editor to display a message if the left arrow key has been pressed:
function keyDown_fn(event:KeyboardEvent) {
if (event.keyCode == 37){
leftArrowSelected=true; } else leftArrowSelected = false;
if (leftArrowSelected == true) {var storeLeftKeyCode:int = 37; trace("left arrow has been written to memory as keyCode ", storeLeftKeyCode)}
}
Review the code you typed in and compare it against the working code below. Correct any typing errors if needed. Copy and paste the following code if you want to ensure there are no errors:
var leftArrowSelected: Boolean
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_fn)
function keyDown_fn(event:KeyboardEvent) {
if (event.keyCode == 37){
leftArrowSelected=true; } else leftArrowSelected = false;
if (leftArrowSelected == true) {var storeLeftKeyCode:int = 37; trace("left arrow has been written to memory as keyCode ",storeLeftKeyCode)}
}
Click the "TestMovie" option in the "Control" menu on the main Flash menu bar. The statement that was typed into the "trace" statement -- "left arrow has been written to memory as keyCode 37" -- appears in the "OUTPUT" display panel when you press the "left arrow" key.