Click to See Complete Forum and Search --> : [AS3]KeyEvent won't work?
SaphuA
08-02-2006, 08:35 AM
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?
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?
Fall_X
08-02-2006, 08:54 AM
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).
SaphuA
08-02-2006, 09:01 AM
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?
Fall_X
08-02-2006, 09:19 AM
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.
SaphuA
08-02-2006, 09:36 AM
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
Fall_X
08-02-2006, 09:48 AM
Mind sharing your key-class?I already did :)
http://board.flashkit.com/board/showthread.php?t=694265 post # 6 and # 7
SaphuA
08-02-2006, 09:57 AM
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
Fall_X
08-02-2006, 10:03 AM
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 :)
jason merchant
08-02-2006, 02:26 PM
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.
tonypa
08-08-2006, 07:10 AM
Moved to AS3 forum.
Fall_X
08-12-2006, 03:08 PM
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.
Incrue
08-27-2006, 07:56 AM
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
cancerinform
08-28-2006, 12:43 AM
I have Mac OS 10.4 (latest update). I tested your movie with the four keys and that looks fine to me.
drfrankius
10-01-2006, 04:40 PM
nm.. deleted the post
flashkit.com
Copyright WebMediaBrands Inc., All Rights Reserved.