View Full Version : Keyboard Event not registering
firdosh
07-02-2006, 06:57 PM
I created a simple AS# project in flex2.
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class Snake extends Sprite
{
public function Snake()
{
this.addEventListener(KeyboardEvent.KEY_DOWN,onKey Press);
}
private function onKeyPress(event:KeyboardEvent):void{
trace("here");
}
}
}
but it does not trace out anything
thanks
cheers :)
firdosh
nirth
07-03-2006, 01:03 AM
are you debugging movie or just running it?
firdosh
07-03-2006, 06:08 AM
debugging it
nirth
07-03-2006, 03:21 PM
Well that's strange, example from help does not work too.
package {
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.*;
public class KeyBoardEventExample extends Sprite {
private var bgColor:uint = 0x00CCFF;
private var size:uint = 80;
public function KeyBoardEventExample() {
var child:Sprite = new Sprite();
child.graphics.beginFill(bgColor);
child.graphics.drawRect(0, 0, size, size);
child.graphics.endFill();
addChild(child);
stage.focus = child;
child.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
child.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
private function keyDownHandler(event:KeyboardEvent):void {
trace("keyDownHandler: " + event.keyCode);
trace("ctrlKey: " + event.ctrlKey);
trace("keyLocation: " + event.keyLocation);
trace("shiftKey: " + event.shiftKey);
trace("altKey: " + event.altKey);
}
private function keyUpHandler(event:KeyboardEvent):void {
trace("keyUpHandler: " + event.keyCode);
}
}
}
nirth
07-03-2006, 03:28 PM
I dont know exactly why this does not work on root and child, but this works fine on Stage, try this
ackage {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class Snake extends Sprite
{
public function Snake()
{
this.stage.addEventListener(KeyboardEvent.KEY_DOWN ,onKeyPress);
}
private function onKeyPress(event:KeyboardEvent):void{
trace("here");
}
}
}
firdosh
07-03-2006, 06:23 PM
thanks..yea I tried that and it worked but I was wondering why it would not work for the main class itself even though its extending Sprite.
I also tried to create some graphics and see if that worked but no luck.
firdosh
nirth
07-03-2006, 07:39 PM
i did the same thing, also tried to use setFocus.
but no result, i'll dig the UIComponent and Application to see how they work, they using keyboard, and it is works there perfectly
hobbis
09-21-2006, 03:18 PM
I had the same problem. It wouldn't work on a class instance or not even if I set the stage.focus to the movieclip asset. So in the end I had to make do with this:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);
m_asset.addEventListener(Event.ENTER_FRAME, onEnter);
private function onKeyDownEvent(event:KeyboardEvent):void
{
//blah blah...
}
alpha betty
12-19-2007, 11:53 PM
While you are testing the movie, click on "Disable Keyboard Shortcuts" in the CONTROL menu. Then keyboard events are captured and traced. This drove me crazy too, I hope that helps.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.