Skip navigation
Ollie Dee
Currently Being Moderated

Help with Keyboard Event

Apr 29, 2013 12:51 PM

Tags: #3.0 #actionscript

Hi, I am designing a drum machine in flash pro Cs6 actionscript 3.0  and I am trying to add a keyboard event that will trigger a button event that I have all ready created, I can create the event to play the sound but this is not what I want, can I call the click event so the button will move on the key event triggering the sound, here is an example of one of my buttons code... btn, thank you if anyone can help.

 

 

 

import flash.events.KeyboardEvent;

/*

 

 

playsound2.fla

 

 

playsound with extra functionality:

 

 

textbox added.

 

 

Displays ID3 info of the sound

 

 

*/

 

 

btn.addEventListener(MouseEvent.CLICK, playSomeSound);

 

 

function playSomeSound(e:MouseEvent)

{

 

 

          var req:URLRequest = new URLRequest("Savage.mp3");

 

 

          var s:Sound = new Sound(req);

 

 

          s.addEventListener(Event.COMPLETE, onSoundLoaded);

 

          s.addEventListener(Event.ID3, onID3Info);

 

 

}

 

 

function onSoundLoaded(event:Event):void

{

 

 

          var localSound:Sound = event.target as Sound;

 

 

          localSound.play();

 

 

}

 

 

function onID3Info(e:Event):void

{

 

 

          var id3Props:ID3Info = e.target.id3;

          var sOut:String = txt.text = "";

 

 

          for (var propName:String in id3Props)

          {

 

 

                    sOut = propName + "=" + id3Props[propName] + "\n";

                    trace(sOut);

                    txt.appendText(sOut);

 

 

          }

          txt.text = id3Props.comment;

 

 

}

 

 

var ba:ByteArray = new ByteArray();

var gr:Sprite = new Sprite();

gr.x = 5;

gr.y = 50;

addChild(gr);

 

 

var time:Timer = new Timer(50);

time.addEventListener(TimerEvent.TIMER, timerHandler);

time.start();

 

 

function timerHandler(event:TimerEvent):void

{

          SoundMixer.computeSpectrum(ba, false);

          var i:int;

          gr.graphics.clear();

          gr.graphics.lineStyle(2, 0xFF0000);

          gr.graphics.beginFill(0x00ff00);

          gr.graphics.moveTo(10, 10);

 

 

          //  draw a circle.;

 

 

          var w:uint = 15;

          for (i=0; i<512; i+=w)

          {

                    var t:Number = ba.readFloat();

                    var n:Number = (t * 200);

                    gr.graphics.drawCircle(i, 0, -n);

          }

}

 
Replies
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 29, 2013 1:11 PM   in reply to Ollie Dee

    you can use the following to trigger your button event:

     

    btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 29, 2013 3:46 PM   in reply to Ollie Dee

    just copy and paste the code i showed.

     

    it just needs to be pasted within the scope of btn.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 30, 2013 8:23 AM   in reply to Ollie Dee

    then add a keyboardevent listener to execute that code:

     

    this.addEventListener(KeyboardEvent.KEY_DOWN,keydownF);

     

    function keydownF(e:KeyboardEvent):void{

    if(e.keyCode==Keyboard.SPACE){

    btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

    }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points