# How to add keyboard interaction?

**URL:** https://forum.kirupa.com/t/how-to-add-keyboard-interaction/438542
**Category:** flash
**Created:** [December 18, 2014, 6:00pm UTC](https://forum.kirupa.com/t/how-to-add-keyboard-interaction/438542 "2014-12-18T18:00:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![withinboy2z](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@withinboy2z](https://forum.kirupa.com/u/withinboy2z)
#### Post date: [December 18, 2014, 6:00pm UTC](https://forum.kirupa.com/t/how-to-add-keyboard-interaction/438542/1 "2014-12-18T18:00:24Z")

</div>

I got 16 different buttons in this application, each button playing different kind of sound. Currently i only able to use mouse click to start playing sound, and click again to stop, but now i wish to add shortcut key to access those track buttons, like pressing “q” for track 1, “w” for track 2. Pressing “q” for the first time will start the sound and animation, press again will stop the sound and animation. Currently I’ve implemented this code, there are no errors but it won’t work somehow, what is the problem?

[CODE]private var \_ambientTracks:Array = [ambient1, ambient2, ambient3, ambient4];  
private var \_effectTracks:Array = [effect1, effect2, effect3, effect4];  
private var \_melodyTracks:Array = [melody1, melody2, melody3, melody4];  
private var \_beatTracks:Array = [beat1, beat2, beat3, beat4];

//handle click on track invoke start / stop functions  
private function onTrackClicked(event:MouseEvent):void {  
var track:Sprite = event.currentTarget as Sprite;  
var trackName:String = track.name;  
if (trackName in \_playingTracks) {  
stopTrack(track);  
delete \_playingTracks[trackName];  
} else {  
startTrack(track);  
\_playingTracks[trackName] = trackName;  
}  
}

//starts track animation and dispatch event for TrackMixer  
private function startTrack(track:Sprite):void {  
Actuate.tween(track, 0.6, {alpha: 0.3}).reflect().repeat();  
dispatchEvent(new ObjectEvent(START\_TRACK, track.name, true));

}

//stop track animation and dispatch event for TrackMixer  
private function stopTrack(track:Sprite):void {  
Actuate.stop(track, “alpha”);  
track.alpha = 1;  
dispatchEvent(new ObjectEvent(STOP\_TRACK, track.name, true));  
}

//trigger keyboard shortcut  
public function setup(stage:Stage):void {  
stage.addEventListener(KeyboardEvent.KEY\_DOWN, keyDown);  
}

```
private var bool:Boolean = false;

private function keyDown(event:KeyboardEvent):void {
    var track1:Sprite = Sprite(String(ambient1));
    if (event.keyCode == 81) {
        if (bool == false) {
            startTrack(Sprite(track1));
            _playingTracks[track1];
            bool = true;
        } else {
            if (bool == true) {
                stopTrack(Sprite(track1));
                delete _playingTracks[track1];
                bool = false;
            }
        }
    }
}[/CODE]
```

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [December 19, 2014, 12:44am UTC](https://forum.kirupa.com/t/how-to-add-keyboard-interaction/438542/2 "2014-12-19T00:44:02Z")

</div>

Do you call setup somewhere? Also, this looks suspect: Sprite(String(ambient1))

---

<div class="post-metadata">

### Author: ![withinboy2z](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@withinboy2z](https://forum.kirupa.com/u/withinboy2z)
#### Post date: [December 19, 2014, 7:26am UTC](https://forum.kirupa.com/t/how-to-add-keyboard-interaction/438542/3 "2014-12-19T07:26:00Z")

</div>

Actually i didn’t call setup from anywhere, but i can’t just simply addEventListner to the keyDown, may i know where should i exactly add the eventListner to keyDown function?
