In FLEX, How can you stop ENTER Key from my Alert being caught by the control that initiated the Alert?
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
{
myT开发者_运维问答extInput.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>
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.
It seems that after the Alert's CloseEvent is called, a KeyboardEvent is dispatched with a target of the textbox (if the enter key was used to close the Alert).
To solve this issue I did like the previous poster mentioned and removed event listener before showing the Alert, but then instead of adding the listener immediately in the CloseEvent function, I wrapped it in a 250ms timeout. While this is a crappy solution, it worked.
wEmail.removeEventListener(KeyboardEvent.KEY_UP, AddEmail);
Alert.show(
"Email '" + wEmail.text + "' is not valid",
"Invalid Email",
Alert.OK,
Sprite(parentApplication),
function(e:CloseEvent):void {
//must delay adding because a second Keyboard.ENTER is
//dispatched after this function if enter was used to close Alert
setTimeout(function():void {
wEmail.addEventListener(KeyboardEvent.KEY_UP, AddEmail);
}, 250);
},
null,
Alert.OK
);
Try event.stopImmediatePropagation
and event.preventDefault
精彩评论