Flex 3: disable backspace and delete in TextArea

I am trying to prevent any key from changing text in Flex TextArea. I do not want the editable property to be false, because I want the carriage to be visible for the current position indicator, so that the user knows where the search that he initiates starts.

I added event handlers for change and textInput, as well as keyUp and keyDown, which execute "event.preventDefault" as well as "event.stopImmediatePropagation". This works fine for most keys except backspace and delete.

Is there any way to prevent this?

+3
source share
4 answers

, , , , , , , , , , :

<?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>
+1

:

<?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>
+2

Why not just insert text when changing?

+1
source

I think I understood the way: in flash, preventDefault do not work for a key event, but they work well for changing an event. You can do something like this fooobar.com/questions/1779386 / ... to avoid any changes, but keep the caret anyway.

0
source

Source: https://habr.com/ru/post/1779384/


All Articles