what is the best way to listen for key events

mike oscar's Avatar

mike oscar

27 Jul, 2011 05:34 PM

Hi All
I hope someone can help I am creating a adobe air for android app and i have a problem. I need to navigate through tha app using the hardware key on the phone which dispatches the a keyCode : Keyboard.BACK. Now how i have wired everything up is. In the app contex I have added a listenerto the stage see below:

contextView.stage.addEventListener(KeyboardEvent.KEY_DOWN, dispatchEvent);
commandMap.mapEvent(keyboardEvent.KEY_DOWN, Backcommand, KeyboardEvent)

This listens for a keyevent then calls the backCommand which takes action. The back command looks like the below:

public class BackCommand extends Command{

    [Inject]
    public var appModel:AppModel;


    [Inject]
    public var evt:KeyboardEvent;



    public override function execute ( ):void {  


        evt.preventDefault();
        evt.stopImmediatePropagation();

        //make sure we have a section to go back to
        appModel.currHistoryPos--;  


        if(appModel.currHistoryPos <0){
            //reset the var. if we dont it will error
            appModel.currHistoryPos = -1;
        }


        //trace("key registered....   " + evt, "curr back num is:  "+appModel.currHistoryPos);

    if (evt.keyCode == Keyboard.BACK) {
        if (appModel.currHistoryPos >= 0) {

            if (exitSection(appModel.currSection) == true) {

                //if we have reached the top of the app sections either library, browse or create exit the app
                appModel.nextSection = Sections.EXIT;
                dispatch(new AppEvent(AppEvent.SECTION_CHANGE));

            }else {

                //only iof the hardware back key has been pressed do this

                trace("//================ key registered....   " + appModel.currHistoryPos);



                    //tells the app we are using it's buttons to naviagte. This is used for the history memory so when back is pressed we can go back to previous section
                    appModel.navType = "back";

                    //grab the new section and set it to be the next section to see then disaptch event to app to jump to that section
                    var backSection:String = appModel.backMemory[appModel.currHistoryPos];

                    appModel.nextSection = backSection;

                    dispatch(new AppEvent(AppEvent.SECTION_CHANGE));

                }
            }


        }

    }

The problem I am having is the back seems to exit the app which it should not do. it should just move to the previous view. So I was wondering am I setting up the keyboard event right. if you could gide me with an example that ould be great.

regards Mike :)

  1. 1 Posted by Michal Wroblews... on 27 Jul, 2011 08:36 PM

    Michal Wroblewski's Avatar

    You should use evt.preventDefault() to prevent the app from closing at Back button and you're using it. Please try to do the same without any command mapping for a check. Just addEventListener and in handler method use preventDefault(), remove evt.stopImmediatePropagation(); by the way.

    And what is contextView.stage.addEventListener(KeyboardEvent.KEY_DOWN, dispatchEvent); doing? Maybe dispatchEvent handler stops immediate propagation?

  2. 2 Posted by Mike oscar on 27 Jul, 2011 08:52 PM

    Mike oscar's Avatar

    Hi michal
    I have tried as without command and it works but this my implementation above does not work. So this leads me to believe that I need to do this differently as I think the preventdefault() is not working the way it is. Is there a different way to do the above so the event is passed directly to the function. Any code will help.

    Regards mike :)

  3. 3 Posted by Michal Wroblews... on 28 Jul, 2011 10:37 AM

    Michal Wroblewski's Avatar

    Looks like not the same instance of the event is passed to the command or something else causes the problem. Do it that way:
    1. Leave listener without the command. It will prevent the default behavior. Don't use stopImmediatePropagation(). 2. Use your own logic as you described in your command. You don't have to preventDefault here.

    Please debug your application and place breakpoints in both command and key listener. Check if events are the same instances (compare memory pointers)

    Thanks
    Mike

  4. 4 Posted by Stray on 28 Jul, 2011 10:44 AM

    Stray's Avatar

    The clone() will mean that the events are unlikely to be the same instances.

  5. 5 Posted by Michal Wroblews... on 28 Jul, 2011 10:45 AM

    Michal Wroblewski's Avatar

    When events are cloned?

  6. 6 Posted by Shawn on 29 Jul, 2011 09:46 PM

    Shawn's Avatar

    Why would RL clone the event? Can't it just pass along the original reference to the command?

  7. 7 Posted by Michal Wroblews... on 29 Jul, 2011 09:57 PM

    Michal Wroblewski's Avatar

    Shawn, no idea.

    I'm using SignalCommandMap and if I need to pass an event I just pass it there :) So I haven't faced the problem. Thanks for letting us know. Probably it should be filled as a bug.

    Mike

  8. Support Staff 8 Posted by Robert Penner on 14 Sep, 2011 07:25 PM

    Robert Penner's Avatar

    If you redispatch an Event, EventDispatcher automatically clones it, because the Event's target and currentTarget properties could be different.

  9. Ondina D.F. closed this discussion on 21 Nov, 2011 09:09 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac