1 Reply Latest reply on Sep 2, 2007 10:49 AM by MacFlex

    Mock Keyboard Event Dispatcher


      I was hoping someone could help me figure out how to make a TextArea/Input accept KeyboardEvent's from a custom EventDispatcher so the TextArea/Input reacts just like someone was typing from a Keyboard? I want to record a set of user actions, Keyboard and Mouse events then play them back automatically using a custom dispatcher so my interface reacts to the recorded script as if someone were actually doing the actions manually.

      I have tried creating a custom TextArea and override the keyUpHandler and keyDownHandler methods, creating a custom EventDispatcher and tying them together, but to no avail. I can get the TextArea to listen to KeyboardEvent.KEY_UP events from my custom dispatcher, but this does not end up with the TextArea displaying the Input. Maybe I have to use the TextField from the TextArea, or go down a totally different approach?

      Any help would be much appreciated.

        • 1. Re: Mock Keyboard Event Dispatcher
          MacFlex Level 1
          Further Thought;

          I wrote the following method; it resides in a custom TextArea and is called from an event that is fired from a button click, I ripped the code off from an Automation class in the SDK:


          public function mockDispatch():void {
          var mytext:String = "test";
          var tEvent:TextEvent = new TextEvent(TextEvent.TEXT_INPUT);
          tEvent.text = mytext;

          var ce:Event = new Event(Event.CHANGE);

          for (var i:uint = 0; i < tEvent.text.length; i++) {
          var ke:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN);
          ke.charCode = mytext.charCodeAt(i);
          ke.keyCode = mytext.charCodeAt(i);

          var te:TextEvent = new TextEvent(TextEvent.TEXT_INPUT);
          te.text = String(mytext.charCodeAt(i));

          ke = new KeyboardEvent(KeyboardEvent.KEY_UP);
          ke.charCode = mytext.charCodeAt(i);
          ke.keyCode = mytext.charCodeAt(i);


          Now; even though this means that the TextArea that owns the TextField will have the keyUpHandler and keyDownHandler methods called for each character of text, which kind of simulates someone entering text; no text ever appears in the TextField and consequently the TextArea. Now, I could just add the text to the TextField as a part of the for loop :



          however, this is not really like a user entering text where the caret of the TextArea/Field moves, and I need to detect the specific caret position to accomplish what I want.

          I think I need to dispatch the TextInput/Keyboard events from whatever the TextField has registered a listener with, but not sure what that is, or whether I am on the right track.

          ps; if anyone thinks this is not the right forum and knows of others, please let me know.