Skip to main content
Known Participant
July 30, 2009
Answered

setting textfield focus in ActionScript 3.0

  • July 30, 2009
  • 1 reply
  • 1326 views

I am using an event handler to listen for MOUSE_UP on stage_mc to let the user create text and images on the screen. I have buttons above the stage that set a variable, then the variable determines which action occurs with the mouse event.

I would like the user to be able to enter text, and can create an input text field on the stage, but the user then has to click inside the text field to begin typing. Because of the event handler, this 2nd MOUSE_UP repositions the text field, although it IS successful in setting the focus.

Ideally, I would like for the user to be able to create the input text field by clicking on the stage and then begin typing without having to click a 2nd time. It seems simple enough, but I still don't understand OOP enough to decipher the help pages available within the program.

Does anyone have any suggestions? Here is my code (some of the included variable are set in previous lines):

//Setting up the Text Field parameters

    var outputText:TextField = new TextField();
    var textFormat:TextFormat = new TextFormat();


// Enabling the input tools

function startDraw(event:MouseEvent):void {
    if (currentTool.name == "lineTool_mc") {
            newLine.graphics.moveTo(event.localX, event.localY);
            stage_mc.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);  


    } else if (currentTool.name == "textTool_mc") {          
        textFormat.color = 0x336699;
        textFormat.size = 48;
        textFormat.font = "Times";
        outputText.defaultTextFormat = textFormat;
        outputText.selectable = true;
        outputText.x = event.localX;
        outputText.y = event.localY;
        outputText.background = false;
        outputText.border = true;
        outputText.autoSize = TextFieldAutoSize.LEFT;
        outputText.type = TextFieldType.INPUT;
        designContainer.addChild(outputText);
       
    } else if (currentTool.name == "brushTool_mc") { ...

This topic has been closed for replies.
Correct answer kglad

on mouseup or click, if the mouse is down, you can use:

stage.focus=outputText;

if the mouse isn't down, you can don't need a listener to trigger the above.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 30, 2009

on mouseup or click, if the mouse is down, you can use:

stage.focus=outputText;

if the mouse isn't down, you can don't need a listener to trigger the above.

Known Participant
July 30, 2009

That was too easy. I just don't get it yet. Maybe after a year of these stupid questions OOP will start to make sense to me.

Thanks again.

kglad
Community Expert
Community Expert
July 30, 2009

you're welcome.