Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to cause a Flex 3 button to respond to the enter key?

Tags:

apache-flex

In Flex 3, buttons call their click handler when they are clicked on by the mouse, or when they have the focus and the user depresses the space bar.

Is there a straightforward way to cause Flex 3 buttons with focus to call their click handler when the user presses the enter key?

like image 357
Jim In Texas Avatar asked Jan 20 '09 19:01

Jim In Texas


2 Answers

Sure, you could do something like this:

<mx:Script>
    <![CDATA[
        import mx.controls.Alert;

        private function btn_click(event:MouseEvent):void
        {
            Alert.show("Clicked!"); 
        }

        private function btn_keyDown(event:KeyboardEvent):void
        {
            if (event.keyCode == Keyboard.ENTER)
                btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
    ]]>
</mx:Script>

<mx:Button id="btn" label="Click Me" click="btn_click(event)" keyDown="btn_keyDown(event)" />

... although I'm not a huge fan of dispatching events on objects outside of those objects. A cleaner approach might be to subclass Button, add the listeners and handlers inside your subclass, and then dispatch the click event from within that class. But this should help illustrate the point. Good luck!

like image 131
Christian Nunciato Avatar answered Oct 27 '22 02:10

Christian Nunciato


For something like a login form, you need to actually use an mx:form - here's the code snippet that illustrates it:

<mx:Form defaultButton="{loadButton}">
<mx:TextInput id="feedURL" />
<mx:Button id="loadButton" label="Load" click="someHandler(event)" />
</mx:Form>

Enter the url and hit enter, bam, expected behavior.

Googled from here.

like image 28
Jason Maskell Avatar answered Oct 27 '22 02:10

Jason Maskell