Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't prevent it, but you can reverse it. To do that you add to handlers with different priorities to the same event - KeyboardEvent.DOWN. One will execute before the TextField's , and save selection indexes and the other after, restoring them. Working code follows:</p> <pre><code> import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import mx.controls.TextInput; import mx.events.FlexEvent; public class FooledTextInput extends TextInput { private var ssi : int = 0; private var sei : int = 0; public function FooledTextInput() { super(); this.addEventListener(KeyboardEvent.KEY_DOWN, onBeforeKeyDown, false, 10000); this.addEventListener(KeyboardEvent.KEY_DOWN, onAfterKeyDown, false, -10000); } private function onBeforeKeyDown(e : KeyboardEvent) : void { if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) { ssi = this.selectionBeginIndex; sei = this.selectionEndIndex; } } private function onAfterKeyDown(e : KeyboardEvent) : void { if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) { this.setSelection(ssi, sei); } } } </code></pre> <p>You might want better names for ssi (selection start index) and sei (selection end index), I was to lazy to find some. I think this solution was based on a post of Alex Harui, I don't remember exactly, but he's got a lot of good Flex related stuff on his blog.</p> <p><strong>UPDATE:</strong> For the spark TextInput, preventing is not only possible, it is really easy. All you need to do is catch the event in the capture phase and stopping propagation. Code:</p> <pre><code>package { import flash.events.KeyboardEvent; import flash.ui.Keyboard; import spark.components.TextInput; public class HackedTextInput extends TextInput { public function HackedTextInput() { super(); addEventListener(KeyboardEvent.KEY_DOWN, onBeforeKeyDown, true); } private function onBeforeKeyDown(e:KeyboardEvent) : void { if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) { e.stopImmediatePropagation(); } } } } </code></pre> <p>Note the last parameter in the addEventListener call, which is set to true in order for the handler to be called in the capture phase. Sadly this solution doesn't work for the mx TextInput, only for the spark one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload