Shan’s Simple Examples: Keyboard shortcuts and TextInput
Someone on #flex asked about preventing text from appearing in a TextInput or TextArea, when the user is using a keyboard shortcut. After a little discussion, I came up with a working example. I decided to use shift-space to simplify things.
The first assumption is that you’re listening for keyboard shortcuts at the application level:
Now, you’ve got your input box, and it’s listeners:
if (e.shiftKey && e.keyCode == 32) {
_isShiftSpace = true;
} else {
_isShiftSpace = false;
}
}
private function shortcutPreventer(e:Event):void {
if (_isShiftSpace) {
e.stopImmediatePropagation();
e.preventDefault();
}
}
Note that I had to set a boolean in my shortcutPreprocess method, and then I check for the boolean in shortcutPreventer. The reason being is that the textInput event doesn’t pass in the KeyboardEvent in any way, so I don’t know if there was a key combo pressed.
Working example & source:
http://www.iotashan.com/examples/KeyboardShortcut/
