feklee
feklee

Reputation: 7695

Capture volume up button press?

Can a Firefox OS app detect when the volume up button has been pressed? If so, how?

(maybe it's just a key event on document, haven't tried that yet)

Upvotes: 5

Views: 3937

Answers (1)

dwi2
dwi2

Reputation: 1076

Bug 989198 is the bug to expose keydown/keyup events to apps. And this wiki page describes this new KeyboardEvent dispatching mechanism. Currently on FFOS phone v2.2 and later version, only VolumeUp and VolumeDown key events will get dispatched into apps.

On FFOS 2.2 and later, any apps could listen to keydown or keyup event of volume-up and volume-down buttons. The events dispatched to handlers are regular KeyboardEvents (the same as in desktop browser). When user press volume-down or volume-up, KeyboardEvent with .key = VolumeDown or VolumeUp will get dispatched to focused app. And if you want to prevent system app to process them (i.e. to turn volume down or up), just simply call event.preventDefault() in your key event handler.

Here's an example of keydown event handling:

var keyDownHandler = function(evt) {
  if (evt.key === 'VolumeDown') {
    // process logic of volume-down
  } else if (evt.key === 'VolumeUp') {
    // process logic of volume-up
  }
  evt.preventDefault(); // to stop system app from processing keydown event
};

window.addEventListener('keydown', keyDownHandler);

Upvotes: 3

Related Questions