package{ import flash.events.*; import flash.display.*; import flash.ui.*; /** * This class is merely going to act as a tester for listening for specific keyboard input. * It will act as a baseline to show you how to listen for specific keys, which will be useful * for many projects to come. * * IMPORTANT NOTE ABOUT TESTING THIS CLASS: When testing it from the flash program, in the * player that pops up, you must select "Disable Keyboard Shortcuts" from the control menu * or some of your keys will not register! */ public class KeyboardReader extends MovieClip{ var input:String = ""; public function KeyboardReader(){ //this keyboard event listens for ANY key being pressed //NOT just for the down arrow. KEY_UP listens for a key being released! this.stage.addEventListener(KeyboardEvent.KEY_DOWN, readKey); } /** * This function first passively echoes out the keycode of the pressed key. Use this to figure * out what the keycode is for the key that you need. Then you can have code checking to see if * the user pressed the key you want... see below */ public function readKey(e:KeyboardEvent):void{ trace(e.keyCode + " = " + String.fromCharCode(e.charCode)); //now when we run this program, let's say that we want to listen for the left ctrl key (fire) and //the space bar for jump. //pressing the ctrl key makes the above statement print out 17, and space bar makes it say 32 //so we can do this: if(e.keyCode == 17){ //can't fire yet, we need to see WHICH ctrl key they pressed. Viewing the KeyboardEvent help, we //find a variable called "keylocation" that can help if(e.keyLocation == KeyLocation.LEFT){ this.fire(); } }else if(e.keyCode == 32){ this.jump(); } } public function fire():void{ trace("pew pew!"); } public function jump():void{ trace("sproing!"); } } }