Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I assign a keyboard event to a button?

New Here ,
Aug 19, 2012 Aug 19, 2012

Hi,

I am umbilical cord new to flash cs4 (as3) and I want to know how I can make a a keyboard event enable a button to be pressed. Specifically, I am making a soundboard in which you click on a button and a sound is played. I want to make it so when you press a SPECIFIC key on the keyboard, the button is pressed so then the sound is played.

P.S. I would really just like a straightforward answer and possibly a code I could copy and paste.

I am using Flash CS4 with Actionscript 3.0

Thank you!

TOPICS
ActionScript
5.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 19, 2012 Aug 19, 2012

You cannot press a button using code.  You could make the button as a movieclip and have it animate like it is being pressed, but the keyboard code would need to trigger the sound as well.

Translate
LEGEND ,
Aug 19, 2012 Aug 19, 2012

You cannot press a button using code.  You could make the button as a movieclip and have it animate like it is being pressed, but the keyboard code would need to trigger the sound as well.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 19, 2012 Aug 19, 2012

Is there a way to make it so it just plays the sound when a key is pressed? Like not through a button just straight from the keyboard to the sound.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2012 Aug 19, 2012

Just use a

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

function keyPressed(event:KeyboardEvent):void {

     //trace(event.keyCode);

     if(event.keyCode == ???){

          // play the sound

     }

}

Where I show the "???" you would replace with the keyCode for the key you want pressed for that sound.  To determine the keyCode you can use the trace line that I have commented out.

Note that when testing in Flash you often need to disable the keyboard shortcuts in the Flash Player in order to be able to use alot of the keys.  You will find this option in the Control menu options in the player.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 19, 2012 Aug 19, 2012

awesome. thank you very much.

oh and this might be a dumb question but what do i put in the "play the sound" part? say my sound is sound.mp3, where or how do i specify that i want that to be played when i press a specific key?

thanks...sorry if i may be dragging this out so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 20, 2012 Aug 20, 2012
LATEST

You should try to learn how to find answers yourself.  Try Googling "AS3 Sound tutorial" and you should be able to find a little code that will do what you need.  The help documentation is also a great resource for information and examples.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines