How to add keyboard interaction?

Legacy support with Phidget21
Fresh meat
Posts: 1
Joined: Thu Dec 18, 2014 12:23 pm

How to add keyboard interaction?

Postby withinboy » Thu Dec 18, 2014 12:27 pm

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: Select all

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 =;
    if (trackName in _playingTracks) {
        delete _playingTracks[trackName];
    } else {
        _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,, 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,, 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) {
                bool = true;
            } else {
                if (bool == true) {
                    delete _playingTracks[track1];
                    bool = false;

Return to “ActionScript 3.0 - Flash CS3 / Flex / AIR”

Who is online

Users browsing this forum: No registered users and 1 guest