as3如何抓住焦点选择事件?

时间:2012-09-07 16:12:02

标签: actionscript-3 tabs navigation focus accessibility

是否可以在动画片段上捕捉“焦点选择”(仅使用TAB,ARROWS,ENTER和SPACE键等辅助功能)?

在下面的例子中,我找不到一种方法来捕捉蓝色方块上的“焦点选择”。有什么建议吗?

import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
import flash.events.FocusEvent;

// build red button
var btSprite:Sprite = new Sprite();
btSprite.graphics.beginFill(0xff0000);
btSprite.graphics.drawRect(0,0,20,20);
btSprite.graphics.endFill();
var redBtn:SimpleButton;
redBtn = new SimpleButton(btSprite, btSprite, btSprite, btSprite);
addChild(redBtn);
redBtn.x = redBtn.y = 0;

// build blue square
var blueSquare:MovieClip = new MovieClip();
blueSquare.graphics.beginFill(0x0000ff);
blueSquare.graphics.drawRect(0,0,20,20);
blueSquare.graphics.endFill();
addChild(blueSquare);
blueSquare.x = blueSquare.y = 100; // not sure it does not show over myBtn

// set blue square selectable using TAB key
blueSquare.tabEnabled = true;
blueSquare.focusRect = true;

// listen events
redBtn.addEventListener(MouseEvent.CLICK, onEvent);         // work click / tab select
blueSquare.addEventListener(Event.SELECT, onEvent);         // does not work
blueSquare.addEventListener(MouseEvent.CLICK, onEvent);     // works click only

// catch events on objects
function onEvent(event:Event){
    trace((event.target?event.target.name:'none') + " selected!");
}

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用焦点事件?

FocusEvent.FOCUS_IN

这是一个很好的教程: http://www.kirupa.com/forum/showthread.php?311824-AS3-Simple-focus-example

关于箭头键或空格键或输入,您需要使用键监听器:

blueSquare.addEventListner(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler(event:KeyboardEvent):void {
    switch(event.keyCode){
        case Keyboard.SPACE:
        case Keyboard.ENTER:
            //do your select
            break;

        case Keyboard.DOWN:
            //do what you want when the down arrow is pressed
            break;

    }
}