views:

178

answers:

2

I am having an issue where I show an AlertBox message when the user hits ENTER and the focus is in a text area. The pop up works fine, but when the user hits enter the Alert closes as expected, but the TextArea listener receives the ENTER event from the Alert and pops the dialog up again. I have tried a number of ways to catch and eat the event but so far I have not been lucky. Is there way to accomplish this?

public function init():void
{
    myTextInput.addEventListener(KeyboardEvent.KEY_UP, handleKeyStrokes);
}

public function handleKeyStrokes(evt:KeyboardEvent):void
{
    if(evt.keyCode == Keyboard.ENTER)
    {
        myAlert = Alert.show("This is a test and only a test", "Title", 4, null, alertCallBack);
    }
}

<mx:TextInput id="myTextInput"
              left="600" top="10">

</mx:TextInput>
+1  A: 

When you show the alert, remove the text listener. Add a listener to the alert for when it closes, and in that close listener, re-add the text listener.

Cory Petosky
I tried that by removing the listeners before doing the Alert.Show and adding the listener back in the callback function of the Alert and when I did this the event still was reaching handleKeyStrokes. The other strange behavior is I tried to add a KEY_UP EventListener for the Alert to capture the ENTER key and it does not seem to fire. Should it?
WeeJavaDude
A: 

Try event.stopImmediatePropagation and event.preventDefault

Robusto
Have not tried preventDefault but have tried stopImmediatePropagation and was not successful. The key issue I am struggling with is that the source of the Alert has a Event listener that is looking for the ENTER key so somehow I either need to stop the event at the Alert point or be able to tell the difference between the ENTER done in the control and the ENTER done at the Alert. The strange thing I am seeing is that if I add a KEY_UP listener on the Alert it does not fire when I hit ENTER.
WeeJavaDude