TextField()防止ctrl + a(全选)

时间:2009-02-18 12:49:07

标签: actionscript-3 flex flex3

如何使用可编辑的TextField()来阻止 CTRL + A

3 个答案:

答案 0 :(得分:1)

前面的示例仅适用于Flex Text和TextArea对象,这适用于所有flash.text。*对象。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;

            private var t:TextField;
            private function init():void
            {
                t = new TextField();
                t.height = 80;
                t.width  = 100;
                t.type   = TextFieldType.INPUT;
                t.multiline = true;
                var c:UIComponent = new UIComponent();
                c.addChild( t );
                foo.addChild( c );
                addEventListener( KeyboardEvent.KEY_UP,         edit );
                addEventListener( KeyboardEvent.KEY_DOWN,       edit );
            } 

            private function edit( event:KeyboardEvent ):void
            {
                if( event.type == KeyboardEvent.KEY_DOWN && event.ctrlKey )
                {
                    t.type = TextFieldType.DYNAMIC; // Dynamic texts cannot be edited.  You might be able to remove this line.
                    t.selectable = false; // If selectable is false, then Ctrl-a won't do anything.
                }
                else
                {
                    t.type = TextFieldType.INPUT;
                    t.selectable = true;
                }
            }
        ]]>
    </mx:Script>
    <mx:Canvas id="foo" height="90" width="110" backgroundColor="#FFFFFF" />
</mx:Application>

答案 1 :(得分:0)

未经测试,但也许您可以抓住TextField上的selectAll event并阻止它冒泡,或清除选择(不确定何时触发事件)。

答案 2 :(得分:0)

使用与KeyboardEvent侦听器配对的setFocus函数:

<xml version="1.0"?>
<!-- menus/SimpleMenuControl.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="800" initialize="init()" >
    <mx:TextInput id="nom"/>
    <mx:Script>
        <![CDATA[
            private function init():void
            {
                addEventListener( KeyboardEvent.KEY_UP,     edit );
                addEventListener( KeyboardEvent.KEY_DOWN,   edit );
            } 

            private function edit( event:KeyboardEvent ):void
            {
                if( event.type == KeyboardEvent.KEY_DOWN && event.ctrlKey ) setFocus();
                else nom.setFocus();
                nom.selectionEndIndex = nom.selectionBeginIndex = nom.text.length;
            }
        ]]>
    </mx:Script>

</mx:Application>

setFocus意味着Text对象将不再侦听任何键盘事件。

我不建议使用enabled属性,因为这会使textarea变灰。