Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-12347

[cordova-android] menubutton and searchbutton events are not fired

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: Master
    • Fix Version/s: None
    • Component/s: cordova-android, cordova-js
    • Labels:
    • Environment:

      android

      Description

      I have ionic 2 project. I try to add event handler to the menubutton event:

      document.addEventListener("menubutton", this.onMenuButtonPress, false);

      But when I run my application on Android device and press menu button, my handler is never called.

      I investigated https://github.com/apache/cordova-android/blob/master/cordova-js-src/platform.js and found, that menubutton and searchbutton are not overrided in opposite to volumeup, volumedown:

      // Add hardware MENU and SEARCH button handlers
      cordova.addDocumentEventHandler('menubutton');
      cordova.addDocumentEventHandler('searchbutton');

      function bindButtonChannel(buttonName) {
      // generic button bind used for volumeup/volumedown buttons
      var volumeButtonChannel = cordova.addDocumentEventHandler(buttonName + 'button');
      volumeButtonChannel.onHasSubscribersChange = function()

      { exec(null, null, APP_PLUGIN_NAME, "overrideButton", [buttonName, this.numHandlers == 1]); };
      }
      // Inject a listener for the volume buttons on the document.
      bindButtonChannel('volumeup');
      bindButtonChannel('volumedown');

      To fix issue I have patched the platform.js:

      function bindButtonChannelWithoutSuffix(buttonName) {
      // generic button bind used for volumeup/volumedown buttons
      var volumeButtonChannel = cordova.addDocumentEventHandler(buttonName);
      volumeButtonChannel.onHasSubscribersChange = function() { exec(null, null, APP_PLUGIN_NAME, "overrideButton", [buttonName, this.numHandlers == 1]); }

      ;
      }

      // Add hardware MENU and SEARCH button handlers
      bindButtonChannelWithoutSuffix('menubutton');
      bindButtonChannelWithoutSuffix('searchbutton');

      function bindButtonChannel(buttonName) {
      // generic button bind used for volumeup/volumedown buttons
      var volumeButtonChannel = cordova.addDocumentEventHandler(buttonName + 'button');
      volumeButtonChannel.onHasSubscribersChange = function()

      { exec(null, null, APP_PLUGIN_NAME, "overrideButton", [buttonName, this.numHandlers == 1]); }

      ;
      }
      // Inject a listener for the volume buttons on the document.
      bindButtonChannel('volumeup');
      bindButtonChannel('volumedown');

      Hope it will be enough to fix this issue.

      Thanks in advance!

      BR,
      Anatoly Shirokov

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              anatolys Anatoly Shirokov
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: