Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
pedgarcia
Posts: 119
Registered: ‎03-10-2010
My Device: Bold 9000
My Carrier: Rogers
Accepted Solution

Keyboard events

Hi,

 

I'm doing some tests trying to move a image around the screen. For that, I'm add listeners as follows:

 

 

addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
....
private function key_pressed(e:KeyboardEvent):void {
switch (e.keyCode) {
....

 

 

But the key_pressed function never gets called. 

 

Any idea what I'm doing wrong here?

 

Thanks

 

 



(*) Kudo-me if you really think I helped you!
My plug: I'm doing a Mobile Experiment, reported on my Blog: Check it out!.
---------------------------
Paulo Garcia
http://www.mobilecreators.com - Twitter: http://twitter.com/mobilecreators
Please use plain text.
Developer
RottenOgre
Posts: 274
Registered: ‎11-01-2010
My Device: PlayBook?
My Carrier: WiFi

Re: Keyboard events

Try stage.addEventListener etc.

 

I'm not sure where you're currently adding that eventListener to. If you don't want to have it apply to the whole stage, specify the object you want to attach the eventListener to.

 

That should make things work.

Please use plain text.
Developer
pedgarcia
Posts: 119
Registered: ‎03-10-2010
My Device: Bold 9000
My Carrier: Rogers

Re: Keyboard events

I was adding the listener to the main class, which is a sprite, right?

 

After adding to the stage instead, it worked, although some keys weren't properly detected. I haven't time to check further why.

 

Maybe you could explain to me about that stage. I believe I can understand the concept of the stage object, but not completely.

 

Why I add objects to the sprite (using addChild() without object specification) but the keyboard listener has to be using the stage.

 

Is there specific objects that can handle user input events, and sprite is not one of them?

 

Thanks for your help!



(*) Kudo-me if you really think I helped you!
My plug: I'm doing a Mobile Experiment, reported on my Blog: Check it out!.
---------------------------
Paulo Garcia
http://www.mobilecreators.com - Twitter: http://twitter.com/mobilecreators
Please use plain text.
Developer
RottenOgre
Posts: 274
Registered: ‎11-01-2010
My Device: PlayBook?
My Carrier: WiFi

Re: Keyboard events

Your main class should be a MovieClip I believe.

 

Stage is just a displayObjectContainer that represents the area that the flash player (or I guess the sim now) should be displaying). 

 

You can read up more about it:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Stage.html

 

I believe we have this unusual phenomenon only for the keyboard because as a general rule you want to listen for any keyboard events that happen anywhere, not just on a particular object.  I'm not 100% sure on this though.

 

As for some keys not working, I think you'll find that those are shortcuts in whatever program you're using, so instead of being picked up by your .swf, the program hijacks them to detect what shortcut you're trying to access. I can't remember if just minimizing will do the trick, but certainly if you run the .swf while your authoring program is closed, you should find that all key events will be picked up.

Please use plain text.
Developer
pedgarcia
Posts: 119
Registered: ‎03-10-2010
My Device: Bold 9000
My Carrier: Rogers

Re: Keyboard events

I could dig a little further today, and I've found the the keys were being detected without any problems. By the way, I was using the Sprite as the HelloWorld, but I've found that for game types, the MovieClip suits better. Is that true?

 

The actual problem is that the program was relying on the Keyboard.KEY_UP and Keyboard.KEY_DOWN events:

 

 

addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, key_released);		
stage.nativeWindow.visible = true;

 

 

The key_pressed() function is called normally, but the key_released() never.

 

Is this somehow expected?

 

Thanks

 

 

 



(*) Kudo-me if you really think I helped you!
My plug: I'm doing a Mobile Experiment, reported on my Blog: Check it out!.
---------------------------
Paulo Garcia
http://www.mobilecreators.com - Twitter: http://twitter.com/mobilecreators
Please use plain text.