Spark 按钮 - 为什么按 Enter 键不触发单击事件?

发布于 2024-12-03 07:01:43 字数 109 浏览 1 评论 0原文

我有点困惑为什么这不是默认行为?

那么,如何检测按钮上按下的 Enter 键并触发单击事件处理程序? (例如,在 TextInput 字段上有一个“enter”事件)

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-12-10 07:01:43

编辑:忽略我之前发布的所有内容。

您可以使用 Spark 按钮上的 keyDown 事件并使用 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: 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)); 
            }
        }
醉态萌生 2024-12-10 07:01:43

编辑:回滚到原始帖子,只有在选择按钮时才会触发事件。

当用户将焦点放在按钮上时,会触发 Enter 事件,与键盘 Enter 键无关。如果我没记错的话,Flash 中激活按钮的默认键是空格键。您可以通过执行以下操作来使用 Enter:

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));
}

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文