Michael B Michael B - 2 months ago 30
Android Question

Cordova - menubutton-event doesn't fire

I am developing an Android-Application with cordova. The android 4.4 device is connected with a bluetooth remote control.

With the help of the documentation, I am able to catch some buttons, e.g. the "volume-up"-key:

document.addEventListener("volumeupbutton", onVolumeUpKeyDown, false);

function onVolumeUpKeyDown() {
console.log("Volume up pressed");
}


According to the cordova-documentation, there are some other Eventlisteners for keys available:


  • backbutton

  • menubutton

  • searchbutton

  • startcallbutton

  • endcallbutton

  • volumedownbutton

  • volumeupbutton



I want that the user gets to the settings-page of my application, when he presses the remotes menu-button, but Unfortunately this button doesn't work for me. Here is the description on the cordova site and the sample code:

document.addEventListener("menubutton", onMenuKeyDown, false);

function onMenuKeyDown() {
console.log("Menu pressed");
}


I have found an APK named "keytest", which shows the pressed keys. This app recognizes:

keyCode=KEYCODE_MENU


still, cordova doesn't fire the event... Why?

Screenshot Keytest

Answer

It's not documented, but you have to override the menu button to make it work

add this line

navigator.app.overrideButton("menubutton", true);

Then you can use

document.addEventListener("menubutton", yourCallbackFunction, false);