Kick Lensing
Kick Lensing

Reputation: 23

AIR/as3 stage keylistener overriding input textfield

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?

Upvotes: 2

Views: 1949

Answers (1)

adarshk
adarshk

Reputation: 338

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.

Upvotes: 3

Related Questions