user988765
user988765

Reputation:

I am trying to test if a text input box has focus

I am fairly new to AS3, so here is my problem.

I have two text input boxes p1 and p2. I want to do a conditional test to see if p1 has the focus. If it does not, then p2 must have the focus. Here is some code I am trying to get to work.

if ((Selection.getFocus()) == (p1totalScore.text)){
    p1Score();
}   p2Score();

Thanks for your help.

David

Upvotes: 1

Views: 3264

Answers (2)

JazzCat
JazzCat

Reputation: 1

Adobe will get you 99% of the way: 'http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/managers/FocusManager.html'

I just changed the function to return the name of the control that has focus, instead of the instance name that their example returns.

private function focusChange(e:FocusEvent):void {
var ti_now:InteractiveObject;
ti_now = fm.getFocus();
trace("Focus now: ", ti_now.name);
}

Upvotes: 0

Jason Sturges
Jason Sturges

Reputation: 15955

Without a FocusManager, you could test what the stage is returning for focus:

(If you have textInput1 and textInput2 on the art-board)

import flash.events.MouseEvent;
import fl.controls.TextInput;

var textInput1:TextInput;
var textInput2:TextInput;

stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);

function mouseClickHandler(event:MouseEvent):void
{
    if(stage.focus == textInput1.textField)
        trace("text field 1 has focus.");
    else if(stage.focus == textInput2.textField)
        trace("Text field 2 has focus.");
}

I think a better approach than you're attempting is to add event handlers for focus change:

import fl.controls.TextInput;
import fl.managers.FocusManager;
import flash.events.FocusEvent;

var textInput1:TextInput;
var textInput2:TextInput;

var focusManager:FocusManager = new FocusManager(this);

textInput1.addEventListener(FocusEvent.FOCUS_IN, textInput1FocusHandler);
textInput2.addEventListener(FocusEvent.FOCUS_IN, textInput2FocusHandler);

function textInput1FocusHandler(event:FocusEvent):void
{
    trace("textInput1 has focus.");
}

function textInput2FocusHandler(event:FocusEvent):void
{
    trace("textInput2 has focus.");
}

Upvotes: 2

Related Questions