简体   繁体   English

如何将键盘事件分配给按钮?

[英]How do I assign a keyboard event to a button?

I have made a soundboard in html. 我用html制作了音板。 So when I press a specific button with my mouse a sound gets played. 因此,当我用鼠标按下特定按钮时,就会播放声音。 Now I want to make just like a launchpad when I press a button on my keyboard eg F, then a sound would get played. 现在,当我按下键盘上的某个按钮(例如F)时,我想像启动板一样制作声音。

Hope someone can help me. 希望可以有人帮帮我。

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. 您可以将要为该声音按下的键替换为keyCode。 To determine the keyCode you can use the trace line that I have commented out. 要确定keyCode,可以使用我注释掉的跟踪行。

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. 请注意,在Flash中进行测试时,您通常需要在Flash Player中禁用键盘快捷键,以便能够使用很多键。 You will find this option in the Control menu options in the player. 您可以在播放器的“控制”菜单选项中找到此选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM