Android 在活动级别捕获关键事件时,如何通知NativeScript Vue应用程序?

Android 在活动级别捕获关键事件时,如何通知NativeScript Vue应用程序?,android,nativescript-vue,Android,Nativescript Vue,我已经扩展了androidx.appcompat.app.AppCompatActivity以捕获NativeScript Vue项目中的关键事件。现在,我的问题是如何通知Vue应用程序已捕获密钥。更重要的是,通知Vue应用程序的正确方法是什么(“因为我可以想出一些不太正确的方法来解决问题”)最后,我找到了最简单的解决方案。在activity.android.js中,我实现了一个简单的侦听器模式: androidx.appcompat.app.AppCompatActivity.extend("

我已经扩展了androidx.appcompat.app.AppCompatActivity以捕获NativeScript Vue项目中的关键事件。现在,我的问题是如何通知Vue应用程序已捕获密钥。更重要的是,通知Vue应用程序的正确方法是什么(“因为我可以想出一些不太正确的方法来解决问题”)

最后,我找到了最简单的解决方案。在activity.android.js中,我实现了一个简单的侦听器模式:

androidx.appcompat.app.AppCompatActivity.extend("org.myApp.MainActivity", {
    keyUpListener: null,
    registerListener(name, func) {
        if (name == "keyUp") {
            this.keyUpListener = func;
        }
        else {
            throw new Error(`There's not a type of listener called ${name}.`);
        }
    },
    unregisterListener(name) {
        if (name == "keyUp") {
            this.keyUpListener = null;
        }
        else {
            throw new Error(`There's not a type of listener called ${name}.`);
        }
    },
    onKeyUp: function (keyCode, event) {
        if (this.keyUpListener) {
            let result = this.keyUpListener(keyCode, event);
            if (result != null) {
                return result;
            }
        }
        return superProto.onKeyUp.call(this, keyCode, event);
    },
    [... snip ...]
然后,在我的.vue文件中,我只需向扩展活动注册一个侦听器:

import { android as androidApp } from "tns-core-modules/application";

[... snip ...]

androidApp.foregroundActivity.registerListener(
        "keyUp",
        this.onKeyUp
);
如果有更好的VueNativeScript方法来处理此问题,我很乐意了解它