Detecting a double click on a TextInput control in Flex

by Peter deHaan on June 24, 2008

in TextInput

The following example shows how you can detect when a user double clicks a Flex TextInput control using the doubleClick event.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/24/detecting-a-double-click-on-a-textinput-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function textInput_doubleClick(evt:MouseEvent):void {
                var ti:TextInput = evt.currentTarget as TextInput;
                ti.enabled = !ti.enabled;
                ti.editable = ti.enabled;
            }
        ]]>
    </mx:Script>

    <mx:TextInput id="textInput1"
            text="The quick brown fox jumped over the lazy dog"
            doubleClickEnabled="true"
            doubleClick="textInput_doubleClick(event);" />

    <mx:TextInput id="textInput2"
            text="The quick brown fox jumped over the lazy dog"
            doubleClickEnabled="true"
            doubleClick="textInput_doubleClick(event);" />

</mx:Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/24/detecting-a-double-click-on-a-textinput-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.TextInput;

            private var textInput1:TextInput;
            private var textInput2:TextInput;

            private function init():void {
                textInput1 = new TextInput();
                textInput1.text = "The quick brown fox jumped over the lazy dog";
                textInput1.doubleClickEnabled = true;
                textInput1.addEventListener(MouseEvent.DOUBLE_CLICK,
                            textInput_doubleClick);
                addChild(textInput1);

                textInput2 = new TextInput();
                textInput2.text = "The quick brown fox jumped over the lazy dog";
                textInput2.doubleClickEnabled = true;
                textInput2.addEventListener(MouseEvent.DOUBLE_CLICK,
                            textInput_doubleClick);
                addChild(textInput2);
            }

            private function textInput_doubleClick(evt:MouseEvent):void {
                var ti:TextInput = evt.currentTarget as TextInput;
                ti.enabled = !ti.enabled;
                ti.editable = ti.enabled;
            }
        ]]>
    </mx:Script>

</mx:Application>

{ 7 comments… read them below or add one }

1 cbrueggenolte July 21, 2008 at 2:00 am

Hi,

when you doubleclick you will select a whole word. To remove the selection, add the following befor the closing brace of the function:


ti.setSelection(0,0);

This will select nothing.

g2 cbr

Reply

2 Tom November 20, 2008 at 9:47 am

Quick question, I have built the same application with a basic textinput but on my laptop I cant type number and the ‘%’ char. But on this example I can.

So what are the differences???

Reply

3 peterd November 20, 2008 at 10:19 am

Tom,

Is the restrict property set?

Peter

Reply

4 Tom November 24, 2008 at 12:29 pm

Hi peterd, yes I restrict only number. But I have the same problem with a TextInput w/o any restrict. I have noticed I have this problem on some other flex applications, should be a local setting compiler or something like that?

Reply

5 peterd November 24, 2008 at 12:38 pm

Tom,

It shouldn’t be. Can you please file a bug at http://bugs.adobe.com/flex/ and include a simple test case showing the behavior. Somebody on the Flex SDK QA team can look into it.

Peter

Reply

6 Tom November 25, 2008 at 2:06 am

Ok I found the bug!!! When you set the wmode to ‘opaque’ the keyboard doesnt work properly. Woa, is it normal?

Reply

7 Tom November 25, 2008 at 2:07 am

Apparently, its a famous bug! http://www.novio.be/blog/?p=416

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: