views:

27

answers:

2

Hi Guys,

I want to design a calculator by Flash cs5 , I use appendText Method to write the data in the textfield by the Keyboard . My problem is when I start the application I have to Click on the TextField first then type the numbers . How i can solve it .

Cheers,

Maged

A: 

you can set focus to the text field as soon as it's added to stage.

frame script:

stage.focus = textFieldInstance;

package:

package
{
import flash.display.Sprite;
import flash.events.Event;

public class DocumentClass extends Sprite
 {
 public function DocumentClass()
  {
  addEventListener(Event.ADDED_TO_STAGE, init);
  }

 private function init(evt:Event):void
  {
  removeEventListener(Event.ADDED_TO_STAGE, init);
  stage.focus = textFieldInstance;
  }
 }
}
TheDarkInI1978
please explain more ???
Maged
i've added example code to my answer
TheDarkInI1978
thank you for your help .
Maged
+1  A: 

What type of TextField are you using?! Provided that you have created a dynamic TextField with the instance name of textfield, the following should work.

 textfield.restrict = "0-9";
 textfield.text = "";

 function onKeyBoardEvent( event:KeyboardEvent ):void
 {
      var str:String = String.fromCharCode(event.charCode );
      textfield.appendText( str);
  }
PatrickS