问题
Extended dispatchKeyEvent in Activity for listening volume up/down key press events in android.
(as described here: How to listen for volume up/down key in android with nativescript?)
But can't figure out how to catch this events in app.
This process described here with all events but dispatchKeyEvent: https://docs.nativescript.org/core-concepts/application-lifecycle#android-activity-events
application.android.on( <what should be here?>, function (args) {
console.log("Event: " + args.eventName + ", Activity: " + args.activity);
});
回答1:
activity.android.js
case android.view.KeyEvent.KEYCODE_VOLUME_UP:
application.notify({
eventName:'KEYCODE_VOLUME_UP',
})
// console.log("KEYCODE_VOLUME_UP");
return true;
inside application
const application = require("tns-core-modules/application");
if (application.android) {
application.on('KEYCODE_VOLUME_UP', this.onVolumeUp);
application.on('KEYCODE_VOLUME_DOWN', this.onVolumeDown);
}
来源:https://stackoverflow.com/questions/62381277/how-catch-android-dispatchkeyevent-in-nativescript-app