3 Replies Latest reply on Aug 7, 2011 4:42 PM by drkstr_1

    Force textInput to wait for all characters?

    kid electric Level 1

      I have a problem with getting text from a text box before the text entry is complete.

       

      Here's the issue: I am using a speech-recognition program to convert spoken words to typed text; thus, when I speak a word, it is typed (like via keyboard) into a TextInput field. 

       

      I would like the program to "check" the text in this TextInput when a spacebar keyboard down event is triggered.  So I've tried adding a keyboard event listener like so:

       

      typedInput.addEventListener(KeyboardEvent.KEY_DOWN, checkKey) // turn on keyboard listener

       

      private function checkKey(keypress:KeyboardEvent):void { // handle keypress triggers

      if (keypress.keyCode == 32) {

           // if spacebar is pressed, check the text of the input box to see if it is correct

           if (typedInput.text == nextWord)  trace("Successful word match! Typed input: "+typedInput.text);

           else

           // if incorrect, highlight the word in orange; if already orange, highlight in red and call the word audio

           trace ("Incorrect spoken word. Typed input: "+typedInput.text);

      }

      typedInput.text = "";

       

       

      }

       

       

      So basically it should check the TextInput and compare against a string, once spacebar is pressed.

       

      The problem is... the results are sporadic; sometimes it triggers the spacebar event before being able to get any text in the input; sometimes it might grab the first letter or two.

       

      Another problem might be that this event listener is listening to EVERY keystroke when all I really care about is listening for the spacebar.

       

       

      I've tried adding the keyboard event listener to the state instead of the text input, but it doesn't seem to make any difference.

       

       

      Any ideas on how to get this to work? I'm going to see if I can use my third-party program (GlovePIE) to map spacebar to enter key instead, and then be able to use the "enter" event for the textinput, but otherwise I'm at a loss... either it's taking too long because it has to check every keystroke, or the keystrokes are coming so fast that the event handler is finished before the text input element can even register that it has been input.

       

      Would it be better to use a "change" event on the text handler? If so, how would I go about checking each new character typed into the input?

        • 1. Re: Force textInput to wait for all characters?
          kid electric Level 1

          Hmm, I got it to work okay by using a timer to force a wait, like so:

           

          private function checkKey(keypress:KeyboardEvent):void {

           

          if (keypress.keyCode == 32) {

          // if spacebar is pressed, check the text of the input box to see if it is correct

          var timer:Timer = new Timer(10,1);

          timer.addEventListener(TimerEvent.TIMER_COMPLETE, timeupHandler);

          timer.start();

          }

           

          function timeupHandler(event:TimerEvent):void {

               if (typedInput.text == nextWord) {

               // if correct, change color of the word and increment to the next word on the page

               trace("Successful spoken word match! Typed input: "+typedInput.text);

               }

           

           

               else {

               // if incorrect, highlight the word in orange; if already orange, highlight in red and call the word audio

               trace ("Incorrect spoken word. Typed input: "+typedInput.text);

               }

               typedInput.text = "";

               }

          }

           

           

          However, this really seems like a hack that I'd like to avoid -- for one thing, I don't want to slow down the system any more than necessary (ie. if it only takes 3 milliseconds to get all the letters in place, why wait 10 every time?), and for another it seems like it would be machine-dependent (processor, etc.)  So this is a hack, not a real solution (unless there is no better solution)

           

           

          I will try converting spacebar to enter and see how that works with an "enter" listener on the input.  (But even this will create a small amount of lag)

          • 2. Re: Force textInput to wait for all characters?
            kid electric Level 1

            Well, I haven't been able to figure out a good way to do this, so hackalicious it will have to be.

             

            Even if I tell GlovePIE to type and then wait some time before pressing space/enter, it is a problem because if the keystroke is pressed before the typing has had time to register, only partial input is captured.

             

            So at this point it seems to be a lot of guess and check testing, hoping that I can choose the most efficient waiting period that also allows for the maximum number of cases to work successfully...

            • 3. Re: Force textInput to wait for all characters?
              drkstr_1 Level 4

              Have you tried listening for a changing event and using the provided TextFlowOperation to do what you need? I've had to do so,ething similar for an auto spell check and that was the route I ended up taking.