Tom
Tom

Reputation: 6707

TextField() Prevent ctrl+a (select all)

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

Upvotes: 0

Views: 1102

Answers (3)

cwallenpoole
cwallenpoole

Reputation: 82028

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>

Upvotes: 1

cwallenpoole
cwallenpoole

Reputation: 82028

Use the setFocus function paired with a KeyboardEvent listener:

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

The setFocus means that the Text object will no longer listen to any keyboard events.

I would not recommend using the enabled property as that will gray-out the textarea.

Upvotes: 0

igkuk7
igkuk7

Reputation: 339

Not tested, but perhaps you could catch the selectAll event on the TextField and prevent it bubbling up, or clear the selection (not sure when the event is fired).

Upvotes: 0

Related Questions