最白目
最白目

Reputation: 3644

phonegap - menubutton doesnt fire anything

I have the following code to test the menu button :

<script type="text/javascript" cahrset="utf-8">     
    //wait till device is ready
document.addEventListener("deviceready", onDeviceReady(), false);

//add a menu button event handler
function onDeviceReady() {
    alert('deviceready');
    document.addEventListener("menubutton", onMenuKeyDown(), false);
}
    //fired when menubutton of device is clicked
function onMenuKeyDown() {
    alert('menu key down');
}

</script>

however when I press them menu button, nothing happens. Logcat says nothing, but I guess since this is from tag "web console" it might be useful:

10-13 15:46:16.720: INFO/Web Console(13969): Error in success callback: Network Status1 = TypeError: Cannot read property 'apply' of undefined at :-2167

I followed this Tutorial from Phonegap API Reference but it doesnt seem to handle my menu button presses. any help? thanks a lot in advance, daniel

edit: Maybe this is helpful for you to know whats going on: When I press the menu Button, logcat shows

10-18 09:21:52.560: WARN/WindowManager(152): statePower =normal

also, the event "searchbutton" doesnt work either

Upvotes: 0

Views: 3002

Answers (3)

Sujit Y. Kulkarni
Sujit Y. Kulkarni

Reputation: 1386

Ok, this one got good for me

var menuOpen = true;
var menuDiv = "";
function onLoad(){
        document.addEventListener("deviceready", onDeviceReady, false);
        menuDiv = document.querySelector("#menu");
}
function onDeviceReady(){

            document.addEventListener("menubutton", onMenuKeyDown, false);
}
function onMenuKeyDown() {

                if(menuOpen) {
                    console.log("close the menu");
                    menuDiv.style.display="none";
                    menuOpen = false;
                } else {
                        console.log("open the menu");
                        menuDiv.style.display="block";
                        menuOpen = true;
                }
}

Upvotes: 0

mukama
mukama

Reputation: 979

Check your body tag. Ensure that it calls the onload() function. Like this:

<body onload="onLoad()">

Upvotes: 0

Selosindis
Selosindis

Reputation: 544

From what I can see, it looks like an issue with your listeners. By adding the brackets at the end of the function name, you are invoking the function immediately when the listener is setup, instead of setting a reference for the event to fire later.

Try:

document.addEventListener("deviceready", onDeviceReady, false);

And:

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

Hope this helps!

Upvotes: 3

Related Questions