6 Replies Latest reply: Apr 30, 2013 8:34 AM by Ollie Dee RSS

    Help with Keyboard Event

    Ollie Dee

      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;







      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;









      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";






                txt.text = id3Props.comment;






      var ba:ByteArray = new ByteArray();

      var gr:Sprite = new Sprite();

      gr.x = 5;

      gr.y = 50;




      var time:Timer = new Timer(50);

      time.addEventListener(TimerEvent.TIMER, timerHandler);




      function timerHandler(event:TimerEvent):void


                SoundMixer.computeSpectrum(ba, false);

                var i:int;


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


                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);



        • 1. Re: Help with Keyboard Event
          kglad MVP

          you can use the following to trigger your button event:


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

          • 2. Re: Help with Keyboard Event
            Ollie Dee Community Member

            Thanks kglad for your reply, could you show me how to write this into my code as I tried this earlier and it gave me back an error, i think I just wrote it wrong.

            • 3. Re: Help with Keyboard Event
              kglad MVP

              just copy and paste the code i showed.


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

              • 4. Re: Help with Keyboard Event
                Ollie Dee Community Member

                Thanks..I pasted it bellow the first button event, and when I run the swf, it just plays my sound randomly,I wanted to attach it to a keyboard event where if i click just say spacebar it will click that button and trigger my sound. I can see where your coming from.

                • 5. Re: Help with Keyboard Event
                  kglad MVP

                  then add a keyboardevent listener to execute that code:




                  function keydownF(e:KeyboardEvent):void{


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



                  • 6. Re: Help with Keyboard Event
                    Ollie Dee Community Member

                    Thanks, this was the code I added in the end and it works nicely, It covers my sound and id3 aswell as Im displaying lcd text in a dynamic text box.



                    stage.addEventListener(KeyboardEvent.KEY_DOWN, btndownKey);


                    function btndownKey(event:KeyboardEvent)


                              if (event.keyCode == 83)


                                        var url:String = "Savage.mp3";

                                        var request:URLRequest = new URLRequest(url);

                                        var s:Sound = new Sound();

                                        s.addEventListener(Event.COMPLETE, completeHandler);

                                        s.addEventListener(Event.ID3, onID3Info);