Flex - 用于模拟按键的按钮

时间:2012-03-02 00:53:42

标签: flex events simulate

我有一个带撤消/重做的TextArea,通过ctrl + z,ctrl + y执行。我的应用程序是一个移动应用程序,在软键盘上没有cntrl,所以我想做一个撤消和一个模拟按键的重做按钮。你如何在Flex中模拟键?

1 个答案:

答案 0 :(得分:1)

您可以发送任何您喜欢的事件,包括KeyboardEvents,以便调度模拟ctrl-z(键码26)和ctrl-y(键码25)的KeyboardEvent,您可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="creationCompleteHandler(event)">
    <mx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                this.addEventListener( KeyboardEvent.KEY_UP, keyHandler );
            }

            protected function clickHandler(event:MouseEvent):void
            {
                this.dispatchEvent( new KeyboardEvent( KeyboardEvent.KEY_UP, true, false, 26, 26, 0, true ) );
            }

            protected function keyHandler( e:KeyboardEvent ):void
            {
                trace(e.charCode, e.keyCode, e.ctrlKey);
            }

        ]]>
    </mx:Script>
    <mx:Button id="undoButton" label="undo" click="clickHandler(event)" />
</mx:Application>

this.dispatchEvent( new KeyboardEvent( KeyboardEvent.KEY_UP, true, false, 26, 26, 0, true ) );是键(没有双关语)它表示调度KEY_UP事件,接下来的两个args是起泡和可取消,然后是charCode和keyCode的26,keyLocation的0,最后是true ctrlKey(模拟它正在关闭)。