I'm a little confused as to why this isn't the default behaviour?

So, how do I detect the enter key being pressed on my button and fire the click event handler? (For example on a TextInput field there is an 'enter' event)

Thanks

有帮助吗?

解决方案

EDIT: Ignore everything I posted before.

You can use the keyDown event on the spark button and create an event handler using KeyboardEvent.

        <s:Button label="Submit" keyDown="enter_pressed(event)" id="submit" click="submit_clickHandler(event)"/>

        protected function enter_pressed(event:KeyboardEvent):void { 
            if(event.charCode == Keyboard.ENTER){
                submit.dispatchEvent(new MouseEvent(MouseEvent.CLICK)); 
            }
        }

其他提示

EDIT : rollbacked to original post, event will only be fired if button is selected anyways

The enter event is fired when users sets focus on the button and has nothing to do with the keyboard enter key. If I'm not mistaken, the default key for activating a button in Flash is spacebar. You could use enter by doing something like this :

myButton.addEventListener(KeyboardEvent.KEY_DOWN, onMyButtonKeyDown);

private function onMyButtonKeyDown(event:KeyboardEvent):void
{
    //simulate click if enter pressed
    if(event.keyCode == Keyboard.ENTER)
        myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top