R – TextField() Prevent ctrl+a (select all)

actionscript-3apache-flexflex3

How can I prevent CTRL+A from functioning with editable TextField()

Best Answer

The previous example only works with Flex Text and TextArea objects, this works with all flash.text.* objects.

<?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>