Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture "Enter" keypress with numeric input in Android/Cordova/Knockout

I am creating an Android application using Cordova/PhoneGap 2.5.0, Knockout 2.1.0 and jQuery Mobile 1.3.0.

I have created an input with type "number", this input is databound with Knockout on it's value. It is also databound to a key press event. I'm intending to catch the user pressing the enter key.

<input type="number" data-bind="value: $root.myInputValue, event: { keypress: $root.myInputKeypress }" min="0" step="1" max="29">


self.myInputKeypress = function() {
    var keyCode = (event.which ? event.which : event.keyCode);

    alert(keyCode);

    if (keyCode === 13) {
        //Do work here

        return false;
    }

    return true;
};

When I press an allowed key such as a number the code runs as expected and the pressed keys code is alerted. When I press enter on the keyboard nothing happens, it seems that Android is suppressing events from keys it thinks are not relevant.

Is there a way to change this behaviour so that I can capture the user hitting enter?

like image 753
Andrew Walsh Avatar asked Apr 03 '13 10:04

Andrew Walsh


1 Answers

If you are using Ionic/Angular

<input ng-keypressed="myKeyPressed($event)" ng-model="myField">

self.myKeyPressed = function(keyEvent) {
    if(keyEvent.keyCode == 13) //do things
}
like image 192
Devid Farinelli Avatar answered Oct 31 '22 13:10

Devid Farinelli