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:

application.addEventListener(KeyboardEvent.KEY_UP,shortcutHandler);

Now, you’ve got your input box, and it’s listeners:
<mx:TextArea height="100%" width="95%" keyDown="shortcutPreprocess(event)" textInput="shortcutPreventer(event)"/>

private function shortcutPreprocess(e:KeyboardEvent):void {
  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/

Help make me popular:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Pownce
  • Slashdot
  • StumbleUpon

This entry was posted on Thursday, March 6th, 2008 at 2:58 pm and is filed under Syndicated. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply