AIR/as3 stage keylistener overriding input textfield

*爱你&永不变心* 提交于 2019-12-12 10:06:27

问题


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

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;

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?


回答1:


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.



来源:https://stackoverflow.com/questions/15549669/air-as3-stage-keylistener-overriding-input-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!