views:

44

answers:

1

Buttons in flex can be pressed with the space key, but a client would like to press enter instead of space. This can be achieved by programming each button, but it would be very time consuming.

Does anyone have an idea how to do this in the less amount of time?

thanks.

+2  A: 

I created a KeyPressForwarder that "forwards" the key press as a click:

package com.sophware.backend
{
    import flash.events.IEventDispatcher;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;

    public class KeyPressForwarder
    {   

        public function dispatchAsClickEvent(evt:KeyboardEvent):void
        {       
            if(evt.keyCode == Keyboard.ENTER)
            {           
                var dispatcher:IEventDispatcher = evt.target as IEventDispatcher;
                dispatcher.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
            }           
        }       

    }   
}

And then setup a binding:

<mx:Button
    id="Name"
    keyUp="_keyPressForwarder.dispatchAsClickEvent(event)"
    click="addOrModifyEntry(event)"
    />

You could eliminate the class and just use the function as the concept is generic. Just make sure you have a click handler to handle the forwarded event.

Kaleb Pederson
I guess there is no escaping adding code to each button, thanks
sergiogx
I believe there is one way. You should be able to [Monkey Patch](http://en.wikipedia.org/wiki/Monkey_patch) the button class (i.e. copy its source file into your project, and then change it) and then add the forwarder as per the above. This means that Flex will pickup your Button class before it finds it in the standard library. It's never pretty, but may work for you.
Kaleb Pederson
Yes I was thinking of extending the button and find/replace all buttons, but I´m liking your idea more, I'll try this first. thanks
sergiogx