views:

580

answers:

3

How can I prevent ctrl+a from functioning with editable TextField()

A: 

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

igkuk7
its not for FLEX3, its in FLASH PLAYER 10
Tom
Sorry, I assumed from the tag in your question you were using Flex. I have no other ideas that haven't already been suggested. Hope you find an answer.
igkuk7
I try to be more specific now: my question is for FLEX; but the selectAll event you mentioned is not working with FLEX. Flex is using flash 9, but SelectAll event is available in flash 10 only.
Tom
A: 

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.

Christopher W. Allen-Poole
setFocus() gives : 1180: Call to a possibly undefined method
Tom
if (evt.ctrlKey) stage.focus = this; does nothing to ctrl+a
Tom
Sorry. For some reason, only half of the code appeared above. I fixed it now.If you're working in Flex 3, then setFocus() will work on any UIComponent, but there is a modified version below which will work on a normal flash.text.TextField.
Christopher W. Allen-Poole
+1  A: 

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>
Christopher W. Allen-Poole