1

Currently, I am using HTML, js with phonegap to write an Android application. This is the function I use to catch the enter button on the virtual keyboard:

function handleFormKeypress(e) 
{
var currentInputElement = $(e.target);

if(e.keyCode == 13 || e.keyCode == 10) 
{
    Log("handleFormKeypress - Go pressed")

    //this needs to be checks as passing in the 'submitButton' is optional
    if (e.data != undefined) 
    {
        if (e.data.handler != undefined) 
        {
            e.data.handler();
        }
    }

    currentInputElement.blur();

    e.stopImmediatePropagation();
    return false;
} 

}

As you can see, I catch the keycode of the keyboard. Converting to Android app using Phonegap, it should catch the Go button, or the Next button of the Virtual keyboard.

My input field's type is number:

<input type="number" id="blah blah blah"/>

In this situation, the android virtual keyboard display an numberic keyboard with the next button.

I tested on several Android phones. When I click to the next button, it jump to the next page as I expected. But on some HTC phones, in fact, the HTC Nexus One and the HTC One X, it does nothing.

Anybody has some ideas here?

Thanks in advance.

Long Dao
  • 1,341
  • 4
  • 15
  • 33
  • Have you tried logging the e.keyCode on the HTC what it returns? – JanR Aug 05 '13 at 04:04
  • I already tried it but it return nothing. Seems like on HTC, nothing happen when I press Next button on the Virtual numberic keyboard. Any other idea? – Long Dao Aug 05 '13 at 04:08
  • Hmm, don't have an HTC phone to test, so it's returning nothing? Is it not even firing? – JanR Aug 05 '13 at 04:13
  • Yes, that's why I am really confusing now. It works on Samsung phones, Sony phones but not HTC. I guess there is some specific function which is only on HTC phones with its sense or something like that. – Long Dao Aug 05 '13 at 04:15
  • Yeah weird, sorry that I can't help you further, but I have no way of testing. – JanR Aug 05 '13 at 04:16
  • No problem, mate. I will keep searching for the solution. Hope that there is someway to fix it. Thanks anyway. :) – Long Dao Aug 05 '13 at 04:18
  • 1
    Next key behavior is akin to a tab on keyboard, but it may be handled by Android browser so no event is fired to the DOM. – leesei Aug 05 '13 at 05:19
  • @leesei so in this case, how can I catch the Next key on the Android virtual keyboard? I discovered that it would be fine if the input type is text, but I need it to be number here? – Long Dao Aug 05 '13 at 22:15
  • From @hotSpud's answer and your testing, it may be a bug on HTC's keyboard then. My guess is that you cannot do it in the browser (DOM) and need to have Android Java code to listen to the event. See code snippet [here](http://stackoverflow.com/a/7180062/665507). – leesei Aug 06 '13 at 03:14

1 Answers1

0

How about using both e.keyCode and e.which?

Add an inline if statement to check for both:

function handleFormKeypress(e) 
{
    var currentInputElement = $(e.target);
    var keyCode = (e.keyCode ? e.keyCode : e.which);

    if(keyCode == 13 || keyCode == 10) 
    {
        Log("handleFormKeypress - Go pressed")

        //this needs to be checks as passing in the 'submitButton' is optional
        if (e.data != undefined) 
        {
            if (e.data.handler != undefined) 
            {
                e.data.handler();
            }
        }

    currentInputElement.blur();

    e.stopImmediatePropagation();
    return false;
}
kieranroneill
  • 849
  • 7
  • 9
  • I tried and it still does not work. Any other suggestion? Thanks. – Long Dao Aug 05 '13 at 22:12
  • Hmm, from what event do you call `handleFormKeypress(e)`? Have you tried all the key events: `keydown`, `keypress` and `keyup`? It maybe the case that HTC only fires on a specific key event. – kieranroneill Aug 06 '13 at 12:50
  • I did try everything. Still not works. I guess it is some problem with HTC or Android, according to my researches. :( – Long Dao Aug 07 '13 at 04:59
  • Unfortunately I don't have a HTC phone handy to test. The only thing I can suggest, without a device, is try capturing the `keypress` event and looking at the `e.charCode` value. Make sure it is the `keypress` event because not all browsers store the value in `keyup` or `keydown`. Check [here](http://unixpapa.com/js/key.html) for more information on events. – kieranroneill Aug 07 '13 at 10:29