Flash / Flex / ActionScript/Development/KeyboardEvent

Материал из Web эксперт
Версия от 08:15, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Converting user input to uppercase

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    private var inputfield:TextField = new TextField(  );
    public function Main() {
      inputfield.text = "";
      inputfield.width  = 150;
      inputfield.height = 30;
      inputfield.border     = true;
      inputfield.background = true;
      inputfield.type = TextFieldType.INPUT;
      addChild(inputfield);
      inputfield.addEventListener(TextEvent.TEXT_INPUT, textInputListener);
    }
    private function textInputListener (e:TextEvent):void {
      e.preventDefault(  );
      inputfield.replaceText(inputfield.caretIndex,
                             inputfield.caretIndex,
                             e.text.toUpperCase(  ));
      var newCaretIndex:int = inputfield.caretIndex +  e.text.length;
      inputfield.setSelection(newCaretIndex, newCaretIndex);
    }
  }
}



Determining the Character Associated with a Key

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  import flash.ui.*;
  public class Main extends Sprite {
    private var keyoutput:TextField = new TextField(  );
    public function Main() {
      keyoutput.text = "Press any key...";
      keyoutput.autoSize = TextFieldAutoSize.LEFT;
      keyoutput.border     = true;
      keyoutput.background = true;
      addChild(keyoutput);
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    }
    private function keyDownListener (e:KeyboardEvent):void {
      keyoutput.text = "The key code for the key you pressed is: "
                       + e.keyCode + "\n";
      keyoutput.appendText("The character code for the key you pressed is: "
                           + e.charCode + "\n");
      keyoutput.appendText("The character for the key you pressed is: "
                           + String.fromCharCode(e.charCode));
    }
  }
}



Handling keyboard events for a particular object

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var rect1:Sprite = new Sprite(  );
      rect1.graphics.lineStyle(1);
      rect1.graphics.beginFill(0x0000FF);
      rect1.graphics.drawRect(0, 0, 75, 75);
      rect1.tabEnabled = true;
      var rect2:Sprite = new Sprite(  );
      rect2.graphics.lineStyle(1);
      rect2.graphics.beginFill(0x0000FF);
      rect2.graphics.drawRect(0, 0, 75, 75);
      rect2.x = 200;
      rect2.tabEnabled = true;
      addChild(rect1);
      addChild(rect2);
      rect1.addEventListener(KeyboardEvent.KEY_DOWN, rect1KeyDownListener);
      rect2.addEventListener(KeyboardEvent.KEY_DOWN, rect2KeyDownListener);
    }
    private function rect1KeyDownListener (e:KeyboardEvent):void {
      Sprite(e.target).x += 10;
    }
    private function rect2KeyDownListener (e:KeyboardEvent):void {
      Sprite(e.target).rotation += 10;
    }
  }
}



Handling keyboard events globally

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main (  ) {
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    }
    private function keyDownListener (e:KeyboardEvent):void {
      trace("A key was pressed.");
    }
  }
}



KeyboardEvent Basics

 
package
{
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    public class Main extends Sprite
    {
        private var sprite:Sprite = new Sprite();
        public function Main()
        {
            sprite.graphics.beginFill(0xFF0000, 1);
            sprite.graphics.drawRect(0, 0, 20, 20);
            sprite.graphics.endFill();
            addChild(sprite);
            this.stage.addEventListener(KeyboardEvent.KEY_UP,moveSprite);
        }
        private function moveSprite(keyEvent:KeyboardEvent):void
        {
            switch (keyEvent.keyCode)
            {
                case 37:
                    sprite.x--;
                    break;
                case 38:
                    sprite.y--;
                    break;
                case 39:
                    sprite.x++
                    break;
                case 40:
                    sprite.y++;
                    break;
                default:
                    break;
            }
        }
    }
}



Listen for the keyDown event and traces out the character code for that key.

 
package {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    
    public class Main extends Sprite {
        public function Main(  ) {
            stage.focus = this;
            addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }
        
        private function onKeyDown(event:KeyboardEvent):void {
            trace("key down: " + event.charCode);
        }
    }
}



Multilocation keys

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.ui.*;
  public class Main extends Sprite {
    public static const A_KEY:int = 65;
    public function Main() {
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    }
    private function keyDownListener (e:KeyboardEvent):void {
      if (e.keyCode == Keyboard.SHIFT) {
        if (e.keyLocation == KeyLocation.LEFT) {
          trace("The left Shift key was pressed");
        } else if (e.keyLocation == KeyLocation.RIGHT) {
          trace("The right Shift key was pressed");
        }
      }
    }
  }
}



Reference key code with Keyboard.ESCAPE

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  import flash.ui.Keyboard;
  public class Main extends Sprite {
    private var keyoutput:TextField;
    public function Main() {
      keyoutput = new TextField(  );
      keyoutput.text = "Press any key...";
      keyoutput.autoSize = TextFieldAutoSize.LEFT;
      keyoutput.border     = true;
      keyoutput.background = true;
      addChild(keyoutput);
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    }

    private function keyDownListener (e:KeyboardEvent):void {
      if (e.keyCode == Keyboard.ESCAPE) {
        trace("The Escape key was pressed");
      }
    }
  }
}



Retrieving a pressed key"s key code through KeyboardEvent.KEY_DOWN event

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    private var keyoutput:TextField;
    public function Main() {
      keyoutput = new TextField();
      keyoutput.text = "Press any key...";
      keyoutput.autoSize = TextFieldAutoSize.LEFT;
      keyoutput.border     = true;
      keyoutput.background = true;
      addChild(keyoutput);
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    }
    private function keyDownListener (e:KeyboardEvent):void {
      keyoutput.text = "The key code for the key you pressed is: " + e.keyCode;
    }
  }
}



Understanding keyCodes

 
package
{
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    public class Main extends Sprite
    {
        private var sprite:Sprite = new Sprite();
        public function Main()
        {
            sprite.graphics.beginFill(0xFF0000, 1);
            sprite.graphics.drawRect(0, 0, 20, 20);
            sprite.graphics.endFill();
            addChild(sprite);
            this.stage.addEventListener(KeyboardEvent.KEY_UP,moveSprite);
        }
        private function moveSprite(keyEvent:KeyboardEvent):void
        {
            switch (keyEvent.keyCode)
            {
                case 37:
                    sprite.x--;
                    break;
                case 38:
                    sprite.y--;
                    break;
                case 39:
                    sprite.x++
                    break;
                case 40:
                    sprite.y++;
                    break;
                default:
                    break;
            }
        }
    }
}



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;
      }
    }
  }
}