Can not handle event onKeyDown


#1
import applicationModule = require("application");
import AndroidApplication = applicationModule.android;    

and in ngOnInit:

    let activity = AndroidApplication.startActivity || AndroidApplication.foregroundActivity ;

    activity.onKeyDown =()=>{
        console.log('on key down');
    }

but wen I press volume buttons it doesn’t log the ‘on key down’ but I tested the below code and it worked.

    activity.onBackPressed = () => {
        console.log('on back');
    };

what is the problem with onKeyDown event?


#2

I don’t think it has been exposed, unlike onBackPressed?

see https://docs.nativescript.org/api-reference/classes/application.androidapplication.html


#3

I know this thread is quite old, but I’ve been struggling with this for about 3 days and finally could get it to work.

I just extended the NS Activity following the docs (https://docs.nativescript.org/core-concepts/android-runtime/advanced-topics/extend-application-activity) and added the onKeyDown function.

public onKeyDown(keyCode: any, keyEvent: any): boolean {
    return true;
}

After that I removed and added the android platform and then, I could just override my own onKeyDown function from the component and everything worked like a charm.