0

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;
    }
HaiderSahib
  • 380
  • 2
  • 4
  • 16
  • Possible duplicate of [Capture keys typed on android virtual keyboard using javascript](https://stackoverflow.com/questions/30743490/capture-keys-typed-on-android-virtual-keyboard-using-javascript) – reyiyo Oct 10 '17 at 18:58

1 Answers1

2

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

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

function getInput(e){
    keyData = String.fromCharCode(e.which);
}
Dawson Loudon
  • 6,029
  • 2
  • 27
  • 31