如何在 Activity 级别捕获关键事件时通知 NativeScript Vue 应用程序?
How to notify a NativeScript Vue app when a Key Event is captured at the Activity level?
我已经扩展 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 文件中,我只需注册一个监听器到扩展 Activity:
import { android as androidApp } from "tns-core-modules/application";
[... snip ...]
androidApp.foregroundActivity.registerListener(
"keyUp",
this.onKeyUp
);
如果有更好的Vue或NativeScript方法来处理这个问题,我很想学习一下。
我已经扩展 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 文件中,我只需注册一个监听器到扩展 Activity:
import { android as androidApp } from "tns-core-modules/application";
[... snip ...]
androidApp.foregroundActivity.registerListener(
"keyUp",
this.onKeyUp
);
如果有更好的Vue或NativeScript方法来处理这个问题,我很想学习一下。