For instance, if I have an ordered set of objects [a, b, c, d, e]
with their tabIndex property correctly assigned, and the focus is currently on object c
, I want to change the focus so that:
a) Nothing is focused;
b) Next time I press tab, object a
gets focused.
stage.focus = null
solves开发者_如何转开发 (a), however it remembers the current index causing object d
to be focused when tab is pressed.
Instead of trying to change the index , you could switch the tabEnabled property of the remaining objects to false, when the focus is on the first objet , enabled it again.
This example is not structured in functions, it's just demo code to clarify what I mean
var objects:Array = [a , b , c , d , e];
//set a KeyboardEvent listener &
//increment the currentIndex value when TAB is pressed
var currentIndex:int = -1;
//when you need to reset , disable tabEnabled property
//for remaining objects & reset the currentIndex
for( var i:int ; i < objects.length ; ++i )
if( i > currentIndex )
objects[i].tabEnabled = false;
currentIndex = -1;
stage.focus = null;
Press tab again, focus is on a
//enable objects
for( var i:int ; i < objects.length ; ++i )
objects[i].tabEnabled = true;
the easiest way imho is to set focus to e
and then to null
I'm not sure that this is the best way to go about it, but it definitely works. Basically you keep track of the UI element that is in focus. Then before nulling the focus you store the element currently in focus. Have a look - I tested this with a few TextInputs and a Button:
// you should change the wildcard datatype to match your UI elements
var currentFocus:*;
var lastFocus:*;
btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(evt:MouseEvent):void{
// store the element currently in focus
// so that we can re-select it next time tab is hit
lastFocus = currentFocus;
// setting stage.focus = null will lost keyboard focus
// so we set it to stage
stage.focus=stage
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyReleased);
function onKeyReleased(evt:KeyboardEvent):void{
if (evt.keyCode == Keyboard.TAB){
// if lastFocus exists, set the stage focus accordingly
// update currentFocus and then set lastFocus to null
if (lastFocus){
stage.focus = lastFocus;
currentFocus = lastFocus;
lastFocus = null;
} else {
// keep track of current focus
currentFocus = stage.focus;
}
}
}
This solution doesn't require altering the tabIndex of the items in the tab loop.
Then add the following code to your project:
import flash.events.FocusEvent;
var tab_resetter:MovieClip = new MovieClip();
tab_resetter.tabIndex = 9999;
tab_resetter.addEventListener( FocusEvent.FOCUS_OUT, function() {
stage.removeChild( tab_resetter );
});
function resetTabIndex():void {
stage.addChild( tab_resetter );
stage.focus = tab_resetter;
}
Since tab_resetter
is not on the stage most of the time, it won't be part of the normal tab loop. Once you call resetTabIndex()
, tab_resetter
is inserted into the tab loop and given focus. If you give tab_resetter
a sufficiently high tabIndex
value, hitting tab after calling resetTabIndex()
will give focus to the first item in the tab loop.
精彩评论