AIR / AS3阶段的KeyListener覆盖输入文本框文本框、阶段、AIR、KeyListener

2023-09-08 13:47:38 作者:[妄]

我要建一个移动AIR应用程序(Android的&安培; IOS)与Adobe Flash Builder中4.6和我有这个恼人的问题。

I'm building a mobile AIR app (Android & IOS) with Adobe Flash Builder 4.6 and I'm having this annoying problem.

由于我要'抓'我增加了以下code到我的主类在Android设备背面键:

Because I want to 'catch' the back-key on Android devices I added the following code to my main class:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

private function keyDown(k:KeyboardEvent):void {    
if(k.keyCode == Keyboard.BACK) {
    backClicked(); // function handling the back-action, not important 
    k.preventDefault();
}

现在别的地方 - 嵌套在一些类 - 我有一个文本框:

Now somewhere else - nested in some classes - I've got a textfield:

TF = new TextField();
TF.type = TextFieldType.INPUT;

但是,当我把重点放在软键盘也出现在文本字段,但是我不能键入单个字符。当我禁用的KeyListener:没问题

But when I set focus on the textfield the soft keyboard does appear, but I can't type a single character. When I disable the keylistener: no problem.

好像监听器重写我的输入字段。有没有什么解决办法对此怎么看?

Seems like the listener is overriding my input field. Is there any workaround on this?

推荐答案

我也实现了我的移动应用程序的后退按钮功能,但我用注册keydown事件,只有当我的特定视图被激活,并取消注册时,视图得到解除。

I have also implemented the back button functionality for my mobile apps , but i used to register keydown event only when my particular view is activated and removed the registered when view get deactivated.

in <s:view ....... viewActivate ="enableHardwareKeyListeners(event)" viewDeactivate="destroyHardwareKeyListeners(event)">
// add listener only for android device
if (Check for android device) {
    NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown, false, 0);
    NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp, false, 0); 
    this.setFocus();                    
}


private function destroyHardwareKeyListeners(event:ViewNavigatorEvent):void
{
    if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_DOWN))
        NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown);
    if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_UP))
        NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp);
}

private function handleHardwareKeysDown(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.BACK) {
        e.preventDefault();
        // your code
    } else {

    }
}           

private function handleHardwareKeysUp(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.BACK)
        e.preventDefault();
}

愿这可以帮助你。

May this can help you.