HaiderSahib
HaiderSahib

Reputation: 380

Phonegap: keypress event not fired on Android

I am working on converting a web app to Android app using Phonegap. keypress event is not firing when I press a key on keyboard. I noticed that keydown event is fired instead of keypress but with keyCode and charCode always 0. Please help.

My code is:

    if (document.addEventListener){
        console.log("****** document.addEventListener ******");
        document.addEventListener("keydown",UI.keydown,false);
        document.addEventListener("keypress",UI.keypress,false);
    }
    else if (document.attachEvent)
    {
        console.log("****** document.attachEvent ******");
        document.attachEvent("onkeydown", UI.keydown);
        document.attachEvent("onkeypress", UI.keypress);
    }
    else
    {
        console.log("****** document.onkeypress ******");
        document.onkeydown= UI.keydown;
        document.onkeypress= UI.keypress;
    }

Upvotes: 0

Views: 4363

Answers (1)

Dawson Loudon
Dawson Loudon

Reputation: 6029

I have always used keyup for this. Here is an example:

document.addEventListener('keyup', getInput, false);

function getInput(e){
    keyData = String.fromCharCode(e.which);
}

Upvotes: 2

Related Questions