Up / Left Sensor : KeyboardEvent : Development Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » Development » KeyboardEvent »

 

Up / Left Sensor


 

package {
  import flash.display.*;
  import flash.events.*;
  import flash.ui.*;

  public class Main extends Sprite {
    private var upPressed:Boolean;
    private var leftPressed:Boolean;

    public function Main (  ) {
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
      stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener);
    }

    private function keyDownListener (e:KeyboardEvent):void {
      if (e.keyCode == Keyboard.UP) {
        upPressed = true;
      else if (e.keyCode == Keyboard.LEFT) {
        leftPressed = true;
      }

      if (upPressed && leftPressed) {
        trace("Up Arrow key and Left Arrow key are both pressed");
      }
    }

    private function keyUpListener (e:KeyboardEvent):void {
      if (e.keyCode == Keyboard.UP) {
        upPressed = false;
      else if (e.keyCode == Keyboard.LEFT) {
        leftPressed = false;
      }
    }
  }
}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo Development
» KeyboardEvent