2 Replies Latest reply on Sep 2, 2009 3:50 AM by Scott McNaught

    How to hook delete and backspace key presses in text area?

    Scott McNaught

      Hello all,

       

      So I know I can addEventListener(TextEvent.TEXT_INPUT, onTextInput); to capture any text input events.

       

      How do I capture when someone presses the delete or backspace key?

       

      I have tried addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); with if(event.keyCode == Keyboard.BACKSPACE), but this triggers the event before the change is made to the text area.  Id like to capture the backspace event after the text area is updated.

       

      Any help appreciated.

       

      Also, does anyone know how do you capture cut / copy / paste events?

       

      Thanks

       

      Scott

      http://www.resultswebdesign.com.au/

       

        • 1. Re: How to hook delete and backspace key presses in text area?
          Andrew Rosewarn

          Hi there

           

          Listen for the KeyUp instead, then the event will fire after the change

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Script>
                  <![CDATA[
                      import mx.controls.Alert;
                     
                      private function keyHandler(event:KeyboardEvent):void {
                          if (event.keyCode == Keyboard.BACKSPACE) {
                              Alert.show('detect')
                          }
                      }
                  ]]>
              </mx:Script>
             
              <mx:TextArea id="ta" x="433" y="138" width="205" height="87" keyUp="keyHandler(event)"/>
             
          </mx:Application>

           

          Hope that helps

           

          Andrew

          • 2. Re: How to hook delete and backspace key presses in text area?
            Scott McNaught Level 1

            Thanks a lot for your detailed example, I should have mentioned in my earlier post though the problem with using onkeyup... it gives a latent effect especially if the user is a heavy key presser (ie - holds down the key for a long time).   I need to make the change as soon as the text is changed.

             

            In most languages there are usually 3 events:

            KeyDown, KeyPress, and KeyUp.

             

            I could do what I want if there was a KeyPress event I could listen to...