A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [AS3]KeyEvent won't work?

  1. #1
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,180

    [AS3]KeyEvent won't work?

    Hey peoples,

    Noticed that the Key.isDown()-thingy doesn't work anymore, so I thought we had to use events for this. However, that doesn't seem to work aswell for me?
    code:
    package {

    import flash.display.Sprite;
    import flash.ui.Keyboard;
    import flash.util.trace;
    import flash.events.KeyboardEvent;

    public class example extends Sprite {
    public function example(){
    this.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    }

    private function keyDownListener(e:KeyboardEvent):void {
    trace("Key Down");
    }
    }
    }



    Anyone?

  2. #2
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Yeah, I've had the same problem. I didn't look into it, but maybe it only works after you added the DisplayObject to the displaylist (just a wild guess).
    But I ended up using stage.addEventListener which works perfectly. However, if anyone gets to the bottom of this, keep us posted! (I might experiment with it a bit when I find the time).

  3. #3
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,180
    What do you mean with 'after you added the DisplayObject to the displaylist'?
    I've tried chacning it to stage.... but without succes though.

    Edit:
    Kist kidding, stage... does work!

    Thanks

    But still I'd like to have this work in the class itself, shouldn't the listeners work on any object?

  4. #4
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Well, by adding an object to the displaylist I mean that it has a parent.
    For instance : root.addChild(someObject) - this would add someObject to root's displaylist.
    But still I'd like to have this work in the class itself
    What I do is I pass a reference to the stage to my objects (unless they have direct access to it, like a displayobject that has been parented), and then do stage.addEventListener() from within my object.
    But the reason I haven't looked into this any further is that I made a Key-class myself with an old-school static isDown() function, and I use this approach in it. All the rest of my code just uses this Key-class, and I don't have to worry about it anymore
    shouldn't the listeners work on any object?
    Well, no, they shouldn't work on any object, but they should on objects that extend displayobject. However, I have a feeling that internally, these keyevent's also use the stage's events, and if you haven't parented your displayobject, it's stage property is null, so it doesn't work. However, that's just an un-educated guess, lol.

  5. #5
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,180
    Yup, sounds interesting

    Mind sharing your key-class?

    Edit:
    What I do now is the folowing:
    Create an empty array.
    Create an keyDown event, that'll set the index of the keyCode on true.
    Create an keyUp event, that'll set the index of the keyCode on false.
    Create an onEnterFrame event, that'll check if an certain index is true or false.

    However, this only works while pressing only 1 key..

    ~Sph
    Last edited by SaphuA; 08-02-2006 at 08:53 AM.

  6. #6
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Mind sharing your key-class?
    I already did

    http://board.flashkit.com/board/showthread.php?t=694265 post # 6 and # 7
    Last edited by Fall_X; 08-02-2006 at 08:51 AM.

  7. #7
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,180
    Thanks

    It seems we both use quite the same method...
    How's your class reacting to multiple keypresses?'
    Because it doesn't work well with my method.

    ~Sph
    Last edited by SaphuA; 08-02-2006 at 08:59 AM.

  8. #8
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Didn't notice any problems with multiple keypresses. Don't see why it should be a problem - haven't tried it in a fast action game though, so it's possible that there is a problem. But I'm pretty sure there isn't

  9. #9
    Member
    Join Date
    May 2006
    Posts
    70
    Event listeners will only be triggered if the object is in focus. Movieclips/Sprites can not be easily focused. You would have to do something like stage.focus = myMovieClip; but the result doesn't look too good.

    Try adding a key listener to a text field. The listener will only be triggered if the text field is in focus. If you click outside the text field, it is no longer in focus and the listener will not be triggered.

    Most of the time, you will want to globally listen for events using stage.addEventListener like Fall_X said. This way all key presses will trigger the event.
    Last edited by jason merchant; 08-02-2006 at 01:29 PM.

  10. #10
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Moved to AS3 forum.

  11. #11
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    I just noticed a problem with this method : if you click another window, and release the key there, flash doesn't dispatch the keyUp event.
    My solution is to use the Stage's deactivate event to reset my arrays.

  12. #12
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    Using stage.addEventListener i found a little delay,i wonder if its just my computer
    I mean, when you press > and keep pressing, it seems like the player waits a little to do the thing
    Its not such a big wait, but in a game its important
    Attached Files Attached Files

  13. #13
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    I have Mac OS 10.4 (latest update). I tested your movie with the four keys and that looks fine to me.
    - The right of the People to create Flash movies shall not be infringed. -

  14. #14
    Member
    Join Date
    Apr 2004
    Posts
    49
    nm.. deleted the post
    Last edited by drfrankius; 10-01-2006 at 03:49 PM.

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