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

发布于 2024-07-14 07:54:25 字数 62 浏览 6 评论 0原文

如何防止 CTRL+A 与可编辑 TextField() 一起使用

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2024-07-21 07:54:25

前面的示例仅适用于 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>

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>
篱下浅笙歌 2024-07-21 07:54:25

未经测试,但也许您可以捕获 < TextField 上的 code>selectAll 事件 并防止其冒泡,或清除选择(不确定事件何时触发)。

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

澜川若宁 2024-07-21 07:54:25

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

<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 对象将不再侦听任何键盘事件。

我不建议使用启用属性,因为这会使文本区域变灰。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文