dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: catching/using keys in CS3

  1. #1
    Senior Member
    Join Date
    Apr 2008
    Location
    Nottingham, UK
    Posts
    348

    catching/using keys in CS3

    hi all, first post on these forums so be gentle!

    I'm a director user about to take the plunge and convert to flash/AS3.

    i have a presentation-style application, and i'd like to advance to an arbitrary frame of the main timeline on a keypress, eg left arrow key.

    as i'm not in work at the mo, i'm paraphrasing:

    function nextFrame(evt:keyBoardEvent):void {
    if (evt.keycode == (theLeftArrowI'veForgottenTheCodeFor)){

    gotoAndStop(currentFrame +1);
    }

    else {

    trace("not the right keycode");

    }
    }

    addEventListener(keyBoardEvent.KEY_DOWN, nextFrame);

    so i'm guessing it's somehting to do with how Flash handles keyboard events... Any advice gratefully received!

    PS i know i should be using classes and keeping my script off the timelines...but i'm still learning. And I have a tight deadline, so that's for the next project!

    Dan

  2. #2
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Hey, Dan,

    There's nothing wrong with coding this on the timeline.
    Code:
    stop();
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    
    function keyPressed(event:KeyboardEvent):void {
    	if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.LEFT) {
    		stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    		stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    		if (event.keyCode == Keyboard.RIGHT && currentFrame < totalFrames) {
    				gotoAndStop(currentFrame + 1);
    		} else if (event.keyCode == Keyboard.LEFT && currentFrame > 1) {
    				gotoAndStop(currentFrame - 1);
    		}
    	}
    }
    function keyReleased(event:KeyboardEvent):void {
    	if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.LEFT) {
    		stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
    		stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    	}
    }
    The logic goes like this. Start off listening for a keypress. As soon as you get one, detect if it might be the right or left arrow. If it's either one of those, remove the event listener for keyDown, and start listening for keyUp. The reason for turning the listener off is to avoid the key repeat rate. As long as you hold down a key, that event will fire repeatedly, but you only want your action to happen once per keyPress. Next, it's determined which of the two arrow keys was pressed. If it's the right arrow key AND we're not already on the last frame, then go to the next frame. If it's the left arrow key AND we're not already on frame 1, then go the previous frame. When the key is released AND it's one of the arrow keys we're concerned with, remove the keyUp listener and reinstate the keyDown one.

  3. #3
    Senior Member
    Join Date
    Apr 2008
    Location
    Nottingham, UK
    Posts
    348
    i wondered what i was doing wrong!

    thanks for the help, i'll test later on today.
    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center