AS3 Detecting key presses


/ Published in: ActionScript 3
Save to your folder(s)

Use this to detect key presses and respond to them.


Copy this code and paste it in your HTML
  1. import flash.ui.Keyboard;
  2. import flash.events.KeyboardEvent;
  3.  
  4. stage.addEventListener(KeyboardEvent.KEY_DOWN, onStage_KEY_DOWN);
  5. stage.addEventListener(KeyboardEvent.KEY_UP, onStage_KEY_UP);
  6.  
  7. function onStage_KEY_DOWN(event:KeyboardEvent):void
  8. {
  9. trace("onStage_KEY_DOWN");
  10. if (event.keyCode == Keyboard.SPACE) {
  11. trace("Keyboard.SPACE");
  12. }
  13. }
  14.  
  15. function onStage_KEY_UP(event:KeyboardEvent):void
  16. {
  17. trace("onStage_KEY_UP");
  18. if (event.keyCode == Keyboard.SPACE) {
  19. trace("Keyboard.SPACE");
  20. }
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.