【问题标题】:AIR/as3 stage keylistener overriding input textfieldAIR/as3 阶段按键侦听器覆盖输入文本字段
【发布时间】:2013-03-11 02:08:40
【问题描述】:

我正在使用 Adob​​e Flash Builder 4.6 构建移动 AIR 应用程序(Android 和 IOS),但遇到了这个烦人的问题。

因为我想在 Android 设备上“捕捉”返回键,所以我将以下代码添加到我的主类中:

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();
}

现在其他地方 - 嵌套在某些类中 - 我有一个文本字段:

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

但是当我将焦点放在文本字段上时,确实会出现软键盘,但我无法输入单个字符。当我禁用 keylistener 时:没问题。

似乎监听器正在覆盖我的输入字段。有什么解决方法吗?

【问题讨论】:

  • keyDown 函数中插入代码中断,进行设备上调试,并重现您的过程以获取错误。查看它是否将k.keyCode 注册为后退按钮。如果是,你需要弄清楚为什么会这样。如果是这种情况,您可能在 AIR SDK 中发现了一个错误。

标签: android ios actionscript-3 air keylistener


【解决方案1】:

我还为我的移动应用程序实现了后退按钮功能,但我曾经只在我的特定视图被激活时注册 keydown 事件,并在视图被停用时删除注册的事件。

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();
}

希望对您有所帮助。

【讨论】:

  • 是的,我对事件做了类似的解决方法:this.addEventListener(GtpEvent.TF_FOCUS, TFFocus); this.addEventListener(GtpEvent.TF_NOFOCUS, TFUnfocus);私有函数 TFFocus(g:GtpEvent):void { stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown); } 私有函数 TFUnfocus(g:GtpEvent):void { stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); }
猜你喜欢
  • 2021-02-11
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多