1 Reply Latest reply on Jan 12, 2008 8:45 PM by chris.huston.t10

    Event handler behavior

    apo11o1
      Simple question:
      Note the line in the handler which is commented... if this remains commented, the text box is not cleared when I press the 'A' key. However, if I allow that alert to occur, the textbox is cleared. This does not make sense to me... what don't I understand here?

      Thanks




      DeleteTextArea.as
      quote:

      package components
      {
      import mx.controls.TextArea;
      import flash.events.KeyboardEvent;
      import mx.controls.Alert;

      public class DeleteTextArea extends TextArea {
      public function DeleteTextArea() {
      super();
      addEventListener("keyDown", myKeyDown);
      }

      private function myKeyDown(eventObj:KeyboardEvent):void {
      if (eventObj.keyCode == 65) { // 'A' key
      text = "";
      //Alert.show('Alert'); // if uncommented, text area is cleared, else not cleared
      }
      }
      }
      }


      Test.mxml
      quote:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:cp="components.*" layout="absolute">
      <cp:DeleteTextArea/>
      </mx:Application>


        • 1. Re: Event handler behavior
          chris.huston.t10 Level 3
          I think the problem is that the event that updates the textArea text value is occuring after the keyDown event. I can think of two ways to get around this. The first is to use callLater to clear the text after the other events have finished. The second way is to use a keyUp listener which fires after the textArea value has changed. These two approaches are listed below:

          public function DeleteTextArea()
          {
          super();
          addEventListener("keyDown", myKeyDown);
          addEventListener("keyUp", myKeyUp);
          }

          private function myKeyDown(eventObj:KeyboardEvent):void {
          if (eventObj.keyCode == 65) {
          callLater(clearText);
          }
          }

          private function clearText():void {
          this.text = "";

          }

          private function myKeyUp(eventObj:KeyboardEvent):void {
          if (eventObj.keyCode == 66) {
          clearText();
          }
          }

          I hope this helps.
          Vygo