AS3: setSelection Up Arrow Override

I would like to focus on the end of the TextField when I press the up arrow. I use:

txt.setSelection(txt.text.length,txt.text.length); 

This works great for any key except the up arrow. I believe the up arrow automatically sets the selection to the beginning of the TextField when it is in focus. How can I override this default behavior?

+1
source share
2 answers

I wanted to change the behavior of the home key, here is how I did it:
(The following code should essentially disable the HOME key, but may be modified to make it do something)

 // Create two variables two remember the TextField selection // so that it can be restored later. These varaibles correspong // to TextField.selectionBeginIndex and TextField.selectionEndIndex var overrideSelectionBeginIndex:int = -1; var overrideSelectionEndIndex:int; // Create a KEY_DOWN listener to intercept the event -> // (Assuming that you have a TextField named 'input') input.addEventListener(KeyboardEvent.KEY_DOWN, event_inputKeyDown, false, 0, true); function event_inputKeyDown(event:KeyboardEvent):void{ if(event.keyCode == Keyboard.HOME){ if(overrideSelectionBeginIndex == -1){ stage.addEventListener(Event.RENDER, event_inputOverrideKeyDown, false, 0, true); stage.invalidate(); } // At this point the variables 'overrideSelectionBeginIndex' // and 'overrideSelectionEndIndex' could be set to whatever // you want but for this example they just store the // input selection before the home key changes it. overrideSelectionBeginIndex = input.selectionBeginIndex; overrideSelectionEndIndex = input.selectionEndIndex; } } // Create a function that will be called after the key is // pressed to override it behavior function event_inputOverrideKeyDown(event:Event):void{ // Restore the selection input.setSelection(overrideSelectionBeginIndex, overrideSelectionEndIndex); // Clean up stage.removeEventListener(Event.RENDER, event_inputOverrideKeyDown); overrideSelectionBeginIndex = -1; overrideSelectionEndIndex = -1; } 
+5
source

there is a Prevent Default (livedocs) function that you can apply to an action if it is canceled (which I assume it will) otherwise you can try and catch its stopPropagation instead:

This has not been tested, but should look something like this:

 function buttonPress(ev:KeyboardEvent):void{ txt.setSelection(txt.text.length,txt.text.length); ev.preventDefault(); } 
0
source

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


All Articles