Skip to main content
December 19, 2010
Answered

How do I start and stop the timer using the keyboard.

  • December 19, 2010
  • 1 reply
  • 934 views

Hello, I would really like some help please, I 

would like to run a programme whereby I press a key

that starts the timer and then press another key to stop the timer, the 

delay will be displayed and used to move a object on the stage dependant on the delay.

Thanks

This topic has been closed for replies.
Correct answer waterlovinguy

Attach an event listener to the stage of your movie.

stage..addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

as per http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/KeyboardEvent.html#KEY_DOWN

1. If you want just any key, then you don't need a key code.

var myKeyBoolean = false;

function keyDownHandler(event:KeyboardEvent){

          if(myKeyBoolean == false){

               //start my timer

          } else {

               //cancel and reset my timer

          }

}

2. If you want the same key to do both use a boolean like this:

var myKeyBoolean = false;

function keyDownHandler(event:KeyboardEvent){

     if(event.keyCode == myKey){

          if(myKeyBoolean == false){

               //start my timer

          } else {

               //cancel and reset my timer

          }

     }

}

3. If you want two different keys

function keyDownHandler(event:KeyboardEvent){

     if(event.keyCode == myStartKey){

               //start my timer

          } else if (event.keyCode == myStopKey) {

               //cancel and reset my timer

          }

     }

}

1 reply

waterlovinguyCorrect answer
Participating Frequently
December 21, 2010

Attach an event listener to the stage of your movie.

stage..addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

as per http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/KeyboardEvent.html#KEY_DOWN

1. If you want just any key, then you don't need a key code.

var myKeyBoolean = false;

function keyDownHandler(event:KeyboardEvent){

          if(myKeyBoolean == false){

               //start my timer

          } else {

               //cancel and reset my timer

          }

}

2. If you want the same key to do both use a boolean like this:

var myKeyBoolean = false;

function keyDownHandler(event:KeyboardEvent){

     if(event.keyCode == myKey){

          if(myKeyBoolean == false){

               //start my timer

          } else {

               //cancel and reset my timer

          }

     }

}

3. If you want two different keys

function keyDownHandler(event:KeyboardEvent){

     if(event.keyCode == myStartKey){

               //start my timer

          } else if (event.keyCode == myStopKey) {

               //cancel and reset my timer

          }

     }

}

Participating Frequently
December 22, 2010

One thing you didn't show using the boolean for toggling is that you need to flip the boolean once you start and stop the timer, like so:

function keyDownHandler(event:KeyboardEvent){

          if(myKeyBoolean == false){

               //start my timer

               !myKeyBoolean

          } else {

               //cancel and reset my timer

               !myKeyBoolean

          }

}

At least that's how I think it's done off the top of my head, the whole ! operator that is.

December 24, 2010

Thanks for your help, I couldn't get the !myKeyBoolean to work, got it to

work with:

stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);

var myKeyBoolean = false;

function keyDownHandler(event:KeyboardEvent)

{

if (event.keyCode == 86)

{

bowlTimer.start();

}

else if (myKeyBoolean == false)

{

bowlTimer.stop();

}

}

Thanks,

Mick