在鼠标单击时调用相同的事件处理程序并输入按键

时间:2012-08-31 08:57:10

标签: actionscript-3 flex flex3

我们可以使用相同的事件处理程序来监听鼠标点击事件和按下回车键时调度的事件吗?

感谢。

1 个答案:

答案 0 :(得分:0)

以下代码可以帮助您: -

    <?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private function callHandler(event:*):void
            {
                if ( event.type == KeyboardEvent.KEY_DOWN || event.type == MouseEvent.CLICK)
                { 
                    if (event.type == MouseEvent.CLICK || (event as KeyboardEvent).charCode == 32 )
                    {
                        Alert.show("Clicked", "Alert"); 
                    }

                }
            }

        ]]>
    </fx:Script>

    <s:Button label="Click" x="100" y="100" click="{callHandler(event)}" keyDown="{callHandler(event)}"/>
</s:Application>