Flex 3:在 TextArea 中禁用退格键和删除

发布于 2024-10-07 10:17:08 字数 259 浏览 5 评论 0原文

我试图阻止任何键更改 Flex TextArea 中的文本。我不想将可编辑属性设置为 false,因为我希望插入符号对于“当前位置”指示器可见,以便用户知道他启动的搜索将从哪里开始。

我添加了change和textInput的事件处理程序,以及执行“event.preventDefault”和“event.stopImmediatePropagation”的keyUp和keyDown。这对于大多数键都适用,但退格键和删除键除外。

有什么办法可以阻止这些人做任何事情吗?

I'm trying to prevent any key from altering the text in a Flex TextArea. I don't want to set the editable property to false, because I want the caret to be visible for a 'current position' indicator, so that the user knows where a search he initiates will start from.

I've added event handlers for change and textInput, as well as keyUp and keyDown that do an 'event.preventDefault' as well as a 'event.stopImmediatePropagation'. This works just fine for most keys, with the exception of backspace and delete.

Is there any way to prevent these from doing anything ?

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

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

发布评论

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

评论(4

﹉夏雨初晴づ 2024-10-14 10:17:08

这可能会有所帮助:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            private function onKeyDown(event:KeyboardEvent):void {
                if ( event.keyCode == 8 || event.keyCode == 46 ) {
                    event.preventDefault();
                }
            }
    ]]>
    </mx:Script>
    <mx:TextArea keyDown="onKeyDown(event);" width="100%" height="100%" />
</mx:WindowedApplication>

This may help:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            private function onKeyDown(event:KeyboardEvent):void {
                if ( event.keyCode == 8 || event.keyCode == 46 ) {
                    event.preventDefault();
                }
            }
    ]]>
    </mx:Script>
    <mx:TextArea keyDown="onKeyDown(event);" width="100%" height="100%" />
</mx:WindowedApplication>
2024-10-14 10:17:08

嗯,似乎它确实在浏览器中不起作用,解决方法怎么样,不确定您是否会喜欢它,但除了粘贴之外似乎还实现了您需要的功能:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private var _lastSelStart:Number = 0;
            private var _lastSelEnd:Number = 0;
            private var _lastText:String = null;
            private var _prevent:Boolean = false;

        private function onKeyDown(event:KeyboardEvent):void {
            if ( event.keyCode == 8 || event.keyCode == 46 ) {
                if ( !_prevent ) {
                    _prevent = true;
                    _lastText = txt.text;
                    _lastSelStart = txt.selectionBeginIndex;
                    _lastSelEnd = txt.selectionEndIndex;
                }
            }
        }

        private function onKeyUp( event:KeyboardEvent ):void {
            if ( _prevent ) {
                _prevent = false;
                txt.text = _lastText;
                _lastText = null;
                callLater(txt.setSelection, [_lastSelStart, _lastSelEnd]);
            }
        }

        ]]>
    </mx:Script>
    <mx:TextArea keyDown="onKeyDown(event);" keyUp="onKeyUp(event);" width="100%" height="100%"
        id="txt" />
</mx:Application>

Hmmm, seems like it really doesn't work in the browser, how about a workaround, not sure if you'll like it but seems to be achieving what you need apart from pasting:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private var _lastSelStart:Number = 0;
            private var _lastSelEnd:Number = 0;
            private var _lastText:String = null;
            private var _prevent:Boolean = false;

        private function onKeyDown(event:KeyboardEvent):void {
            if ( event.keyCode == 8 || event.keyCode == 46 ) {
                if ( !_prevent ) {
                    _prevent = true;
                    _lastText = txt.text;
                    _lastSelStart = txt.selectionBeginIndex;
                    _lastSelEnd = txt.selectionEndIndex;
                }
            }
        }

        private function onKeyUp( event:KeyboardEvent ):void {
            if ( _prevent ) {
                _prevent = false;
                txt.text = _lastText;
                _lastText = null;
                callLater(txt.setSelection, [_lastSelStart, _lastSelEnd]);
            }
        }

        ]]>
    </mx:Script>
    <mx:TextArea keyDown="onKeyDown(event);" keyUp="onKeyUp(event);" width="100%" height="100%"
        id="txt" />
</mx:Application>
御守 2024-10-14 10:17:08

为什么不直接在更改时重新插入文本呢?

Why not just reinsert the text on change?

愁杀 2024-10-14 10:17:08

我想我已经找到办法了:
在 Flash 中,preventDefault 不适用于关键事件,但它们适用于
改变事件。您可以执行类似于此 https://stackoverflow.com/a/8078910/1927950 的操作来避免任何修改,但仍然保留插入符号。

I think I've figured out a way:
in flash the preventDefault don't work for key event, but they work well for
changing event. You can do something similar to this https://stackoverflow.com/a/8078910/1927950 to avoid any modification but still keep the caret.

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