Home > Articles > Graphics & Web Design > Dreamweaver & Flash

  • Print
  • + Share This
This chapter is from the book

Lesson 23: Accepting Keyboard Input

The previous example used five buttons to allow you to test the five different portions of the movie clip. Although buttons are a primary source of input into Flash movies, in the world of games, you'll eventually want to accept input directly from the keyboard.

There are two ways to get keyboard input from the user. The first involves a special way to use buttons. The second method uses only code to test whether keys are pressed at any given time.

To use the button method, create a normal Flash button. It doesn't have to be anything special because you'll be hiding it off the Stage. The following sample code can be attached to that button to have it accept the "r" key and use it to move a movie clip:

on (keyPress "r") {
  circle._x++;
}

TIP

Note that keyPress actions attached to buttons are case sensitive. This means that if you are looking for the "r" key to be pressed, the button will not respond to Shift+R.

Example file: Keyboardbutton.fla

For movement, it is often better to look for the arrow keys to be pressed rather than letters. To do this, you can use some special syntax. Here is a longer script that enables the user to move the movie clip in all four directions. You can find this script in the Keyboardbutton.fla movie on the CD.

on (keyPress "<Right>") {
  circle._x++;
}

on (keyPress "<Left>") {
  circle._x--;
}

on (keyPress "<Up>") {
  circle._y--;
}

on (keyPress "<Down>") {
  circle._y++;
}

Although using buttons allows ActionScript to capture individual keypresses, it does not work well in situations where you want fast and fluid movement. In these situations, the Key code object allows you to test the state of any key to see whether it is currently being pressed.

TIP

If you use a button to detect keypresses, you'll notice that the user can hold a key down and the action takes place repeatedly. Most computers are set to repeat keys if the user holds down the key. The delay between the first and second time the key is activated, and the delay between each successive key, is determined by the user's system keyboard settings. You should not rely on this keyboard functionality for games in which you want the user to be able to hold down a key to repeat an action. Instead, use the Key.isDown() function described in this section.

The Key.isDown()function allows you to test a key. For instance, if you want to see whether the "r" key is pressed, you can do this:

if (Key.isDown("r")) {
  circle._x++;
}

Example file: Keyisdown.fla

This code does not have to be and should not be attached to a button. Instead, it should be attached to an "action" movie clip, as is the case in the movie Keyisdown.fla. The code is inside an onClipEvent(enterFrame) handler so that the keyboard is checked every frame. The code looks for the arrow keys again, which are signified by special constants, such as Key.RIGHT.

onClipEvent(enterFrame) {
  if (Key.isDown(Key.RIGHT)) {
    _root.circle._x++;
  }

  if (Key.isDown(Key.LEFT)) {
    _root.circle._x--;
  }

  if (Key.isDown(Key.UP)) {
    _root.circle._y--;
  }

  if (Key.isDown(Key.DOWN)) {
    _root.circle._y++;
  }
}

If you run this example movie, you'll see that the circle behaves in a much more fluid way. The frame rate of the movie has been increased to 120fps to take advantage of as many frames per second as the computer can handle.

So when is it a good idea to use a button to accept keystrokes and when is it a good idea to use the Key object? If you are looking for a single keypress to trigger an event, then use a button. This reacts to the keypress 100% of the time. The Key.isDown() function sees the key pressed only if the key happens to be down when the function is called. So if the user presses down and then lifts up quickly, the keypress could be missed. This can easily happen on slow computers.

The Key.isDown() function, on the other hand, is good for when you want the player to control movement. In this case, you want fluid movement that will continue at a constant rate as long as the player holds down the key. By using Key.isDown(), you can also test for more than one key being pressed at a time, whereas a button reacts to a single keystroke only.

  • + Share This
  • 🔖 Save To Your Account