Search code examples
extjssencha-touchsencha-touch-2

How to add a keydown listener to Ext.field.number?


I wish to add a keydown listener to a number field in Sencha Touch 2 to dynamically check the field for errors rather than waiting until the form is submitted. The number field is a component on form. Looking at the official documentation, only the keyup event can be listened to.

Is it possible to listen to the standard javascript keydown event and define this custom listener in the initialization function of the form?


Solution

  • You can add a listener to the input field:

    numberField.getComponent().input.on({
      scope: this,
      keydown: 'onKeyDown'
    });

    Then define what you want to do on the method 'onKeyDown'

    onKeyDown: function(e, obj) {
      //Your code here
    }

    Be careful, I've run into numerous problems trying to use keyboard functions on different devices, particularly with Android. After many struggles with this and the lack of standards on number fields I found building a number pad for data entry (as opposed to the various device keypads) was a better solution. Hope this helps.