The bug-a-lot solution is intriguing - I did not think to use priorities. Instead, I used different phases of the event process. The problem for me was to decide whether the arrow keys should advance or retreat a single character within a given text field or move to the next or previous field in the form.
First, I redefine the processing of stage events for keys:
stage.addEventListener(KeyboardEvent.KEY_UP, typing); // Sneak in before the normal processing handles right and left arrow keystrokes. stage.addEventListener(KeyboardEvent.KEY_DOWN, pretyping, true, 10);
Please note that I will process each key twice - before the system does this wizardry (pretyping is called before KEY_DOWN), so I can see where the carriage is before Flash moves it and after the system processes it ( using the input called after KEY_UP).
private function pretyping(evt:KeyboardEvent):void { var keyString:String = Configuration.getKeyString(evt); if (isFocusInTextBox()) { var tf:TextField = getFocus() as TextField; capturePhaseCaret = tf.caretIndex; } }
getKeyString is my method - all it does is turn these codes into convenient mnemonics. And isFocusInTextBox is a challenge to my focus manager - I replaced the standard focus manager to overcome other problems with Flash. I just need to know if this is a text box or not.
Then I need to process the key after Flash has already moved the caret and maybe even moved to a new field, and after looking at the previous state, decide what Flash is doing and cancel it, and then do what should happen. My typing function has many things that are not needed for this discussion, but the important thing that it does is call allowKeyMapping. allowKeyMapping decides whether the user enters the forward arrow (or down arrow) from the last character position in the text box or enters the back arrow from the very beginning. If so, "enter" will be displayed in the next or previous field, respectively.
/** Prefer default behavior and do not allow certain kestrokes to be reinterpreted by key mappings, such as left and right cursor keys in text boxes. */ private function allowKeyMapping(keyString:String) { var allow:Boolean; // If focus is in a text field, allow right arrow to advance to next field only if caret is at end of text. // Conversely, allow left arrow to back up to previous field only if caret is at beginning of text. // The trick is that the normal processing of keystrokes by TextFields occurs before this method is called, // so we need to know the caret position from before the key was pressed, which we have stored in capturePhaseCaret, set in pretyping. if (isDragging) { allow = false; } else if (isFocusInTextBox()) { var tf:TextField = getFocus() as TextField; if (keyString == Configuration.LEFT_CURSOR_KEY) { allow = tf.caretIndex == 0 && capturePhaseCaret == 0; } else if (keyString == Configuration.RIGHT_CURSOR_KEY) { allow = tf.caretIndex == tf.text.length && capturePhaseCaret == tf.text.length; } else { allow = true; } } else { allow = true; } return allow; }
Sorry, I do not have a compact example. I just thought it was important to emphasize that you can get around the Flash addiction to do something for you, whether you want them to happen or not.