Keyboard Events

Flash can respond to keyboard commands, but when you are working inside Flash, the Flash environment can capture the keyboard input instead of the movie.
When you are testing a Flash movie that uses keyboard input you must:

  • Start the test movie;
  • From the menu of the running movie select Control, Disable Keyboard Shortcuts
  • The keyboard input WILL NOT WORK if you don’t do this!

Start a new Flash ActionScript3 movie and copy the code in the action window.

  stage.addEventListener(KeyboardEvent.KEY_DOWN,  doKeyDown);
  function doKeyDown(e:KeyboardEvent):void {
     trace(e);
  } //doKeyDown 

With the movie running and keyboard shortcuts disabled, type a few keys.
You will notice some useful properties of e, the keyboard event:
e.ctrlKey is true if the control key is held;
e.altKey is true if the ALT key is held;
e.shiftKey is true if the shift key is held.
The other property of e is e.keyCode which we can look at to see which key was pressed.

You can try typing various keys in the movie below:

Get Adobe Flash player

NEXT: Using Keys to Move Object