Skip to main content
Participating Frequently
December 18, 2014
Question

How to add keyboard interaction?

  • December 18, 2014
  • 1 reply
  • 360 views

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?

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;

                }

            }

        }

    }

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 18, 2014

assuming you're calling setup from somewhere, use the trace function to debug.

withinboyAuthor
Participating Frequently
December 18, 2014

Actually i just want to addEventListener to the keyDown and define the keyDown function, even i use trace it also doesn't show up anything, that mean the keyCode == 81 is actually not working, do you have any idea or other way to make sure the keyCode is working?

kglad
Community Expert
Community Expert
December 18, 2014

first, add a trace to setup to confirm you're calling it.

then add a trace to keydown to confirm your listener function is being called.

etc.