AS3:setSelection 向上箭头覆盖 [英] AS3: setSelection Up Arrow Override

查看:16
本文介绍了AS3:setSelection 向上箭头覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下向上箭头时关注 TextField 的末尾.我正在使用:

I would like to focus on the end of a TextField when the up arrow is pressed. I'm using:

txt.setSelection(txt.text.length,txt.text.length);

这适用于除向上箭头以外的任何键.我相信当它处于焦点时,向上箭头会自动将选择设置为 TextField 的开头.如何覆盖此默认行为?

This works great for any key except the up arrow. I believe that the up arrow automatically sets selection to the beginning of a TextField when it is in focus. How can I override this default behaviour?

推荐答案

我想改变 home 键的行为,我就是这样做的:
(以下代码应该基本上禁用 HOME 键,但可以修改以使其执行任何操作)

I wanted to change the behavior of the home key, this is how I did it :
(The following code should essentially disable the HOME key but can be modified to make it do anything)

// Create two variables two remember the TextField's selection
// so that it can be restored later. These varaibles correspong
// to TextField.selectionBeginIndex and TextField.selectionEndIndex
var overrideSelectionBeginIndex:int = -1;
var overrideSelectionEndIndex:int;

// Create a KEY_DOWN listener to intercept the event ->
// (Assuming that you have a TextField named 'input')
input.addEventListener(KeyboardEvent.KEY_DOWN, event_inputKeyDown, false, 0, true);

function event_inputKeyDown(event:KeyboardEvent):void{
    if(event.keyCode == Keyboard.HOME){
        if(overrideSelectionBeginIndex == -1){
            stage.addEventListener(Event.RENDER, event_inputOverrideKeyDown, false, 0, true);
            stage.invalidate();
        }

        // At this point the variables 'overrideSelectionBeginIndex'
        // and 'overrideSelectionEndIndex' could be set to whatever
        // you want but for this example they just store the
        // input's selection before the home key changes it.
        overrideSelectionBeginIndex = input.selectionBeginIndex;
        overrideSelectionEndIndex   = input.selectionEndIndex;
    }
}

// Create a function that will be called after the key is
// pressed to override it's behavior
function event_inputOverrideKeyDown(event:Event):void{
    // Restore the selection
    input.setSelection(overrideSelectionBeginIndex, overrideSelectionEndIndex);

    // Clean up
    stage.removeEventListener(Event.RENDER, event_inputOverrideKeyDown);
    overrideSelectionBeginIndex = -1;
    overrideSelectionEndIndex   = -1;
}

这篇关于AS3:setSelection 向上箭头覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆