Skip to main content
Participant
May 6, 2011
Answered

Acknowledging a keypress once only

  • May 6, 2011
  • 1 reply
  • 698 views

Hi,

Im doing a flash project that is hooked up to a PIR sensor which triggers a keypress to make the SWF play. Only problem is that the sensor keeps triggering the keypress which makes the swf start over and over again. How would I tell actionscript to only recognise the keypress once and to only play the swf once only?

This topic has been closed for replies.
Correct answer Lee_Burrows

Thanks for the quick reply, would removing the event listener work after the key has been pressed once only ya? Like basically instead of the sensor triggering the key G for example, it triggers it by going G G G G again and again as the sensor is a motion detector, so would removing the event listener prevent this from restarting the swf a number of times?


yep - if you remove the listener inside the handler function then it will get removed once a key is pressed. something like this:

stage.addEventListener(KeyboardEvent, handler);

function handler(event:KeyboardEvent):void

{

     stage.removeEventListener(KeyboardEvent, handler);

     //launch swf

}

(once you remove the listener, there is no way for the handler function to get called again, so the swf launch will only happen once)

1 reply

Lee_Burrows
Participating Frequently
May 6, 2011

just setup a variable to determine if swf has already been started, eg:

var flag:Boolean = false;

function keyboardHandler(event:KeyboardEvent):void

{

     if (!flag)

     {

          //start swf here

     }

     flag = true;

}

Lee_Burrows
Participating Frequently
May 6, 2011

...actually, a better method would be to remove the keyboard listener once its no longer required

Participant
May 6, 2011

Thanks for the quick reply, would removing the event listener work after the key has been pressed once only ya? Like basically instead of the sensor triggering the key G for example, it triggers it by going G G G G again and again as the sensor is a motion detector, so would removing the event listener prevent this from restarting the swf a number of times?