2

Is it possible to bind to hardware buttons of mobile device using jQuery mobile? If so, are there any code examples?

Particulary I am interested in binding to mobile device volume up and down buttons and scrolling pages using those buttons.

Here is similar question regarding android and hardware back button Take control of hardware back button jquery mobile

Now I am wondering how to do same thing for volume up and down but crossplatform?

Community
  • 1
  • 1
formatc
  • 4,261
  • 7
  • 43
  • 81

2 Answers2

3

Short answer is no, you can't do that with a jQuery Mobile only.

Now I would assume you are creating a mobile application, and by mobile application I mean hybrid android/iPhone app and not a mobile version of your web app. If I am correct you will probably use a Phonegap/Cordova framework wrapper which will server as a wrapper between your HTML/CSS/JS and a natuve app.

More about Phonegap/Cordova framework can be found here: http://cordova.apache.org/

jQuery Mobile and Phonegap is a standard mix, you can find a lot of how to tutorials.

But back to your question, here's how you can bind volume up and volume down event using Phonegap to jQuery mobile code:

document.addEventListener("volumedownbutton", onVolumeDownKeyDown, false);

function onVolumeDownKeyDown() {
    // Handle the volume down button
}

and:

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

function onVolumeUpKeyDown() {
    // Handle the volume up button
}

Also take a look at my other short article how to handle phonegap events with jQuery mobile:

https://stackoverflow.com/a/14010308/1848600

More about volume up/volume down events can be found here: http://docs.phonegap.com/en/1.2.0/phonegap_events_events.md.html

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • The plan is to use Phonegap (never used it before) later on but since I need to buy iOS developer license, I am holding it off until I finish most of work. Good to know that there is way to catch those events, while we are at it, is it possible to prevent default action ie. speaker dialog on iOS? One thing more confuses me, in Phonegap documentation it says only BlackBerry supports those events? – formatc Jan 12 '13 at 18:18
0

Sorry but Jquery or Jquery mobile controls the browser DOM and UI of your webpage or webapp.. They doesn't control the mobile devices features.. That you have mentioned...

But as far the volume up and down is concerned you can use a jquery plugin for this...

If you are working in Jquery Mobile, just use this...

http://videojs.com/

A HTML5 Video Player that can be used as an alternative of your requirement...

SaurabhLP
  • 3,619
  • 9
  • 40
  • 70
  • 1
    Well it doesn't seem like that crazy idea to be able to listen for hardware input, after all you can listen to for example keypress of keyboard on desktop. I really don't understand how would I use video player to catch volume up/down button keypress? I am looking at source code of player but I can't find part where it catches input.. – formatc Jan 12 '13 at 14:00