如何检查用户是否在文本字段中输入内容?

时间:2016-01-08 09:01:11

标签: actionscript-3 flex air flex4

您如何检查用户是否在输入文本输入(Flex TextArea,TextInput,RichEditableText)?

在我的AIR应用程序中,我想在应用程序上侦听键盘事件,但我只想在没有键入文本字段并且没有其他组件处理它时进行响应。

例如,我想要监听 - ERROR - Failed to parse sources 密钥并运行命令。但前提是他们没有输入文本字段。

仅供参考我为A添加了NativeFlexMenu并为其添加了键盘修饰符,即使用户在文本字段中输入,它也会捕获键事件。

1 个答案:

答案 0 :(得分:1)

这在添加到应用程序时似乎有效。这样做的一个问题是,如果文本字段具有焦点,即使您单击应用程序,它也将继续具有焦点。我可能会尝试添加一个单击事件处理程序来更改焦点。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication keyUp="application1_keyUpHandler(event)"
                       keyDown="application1_keyDownHandler(event)">
</s:WindowApplication>

protected function application1_keyDownHandler(event:KeyboardEvent):void {
    var keyCode:int = event.keyCode;
    var applicable:Boolean;
    var focusedObject:Object;
    var target:Object = event.target;

    if (keyCode==Keyboard.A || 
        keyCode==Keyboard.B || 
        keyCode==Keyboard.C) {

        if (!event.controlKey && !event.commandKey) {
            applicable = true;
        }
    }

    if (!applicable) return;

    focusedObject = focusManager.getFocus();

    if (focusedObject is Application || event.target is Stage) {
        isApplication = true;
    }

    //var t:int = getTimer();
    if (target is RichEditableText ||
        focusedObject is IEditableText ||
        focusedObject is SkinnableTextBase ||
        focusedObject is TextField ||
        focusedObject is FTETextField ||
        focusedObject is FlexHTMLLoader ||
        focusedObject is ComboBox) {
        applicable = false;
    }
    //trace("time:" + (getTimer() - t)); takes 0 ms

    if (applicable) {
        if (keyCode==Keyboard.A) {
            // do something
        }
        else if (keyCode==Keyboard.B) {
            // do something
        }
        else if (keyCode==Keyboard.C) {
            // do something
        }
    }
}