nkuhta
nkuhta

Reputation: 11128

ExtJS:: How to add specific "specialkey event" for comma to numberfield?

If want to my nubmerfield take "," and covert it to "." on key up. Numberfield doesn't allow to input "," in it. And I want to handle "specialkey" and add "." manually. But "," isn't special key. How to add it there?

Upvotes: 3

Views: 4287

Answers (2)

Gowtham
Gowtham

Reputation: 21

specialkey: function(field, e){
              // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
              // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
              if (e.getKey() == e.TAB) {
                    //Do specific action
              }
        }

Upvotes: 2

Wilk
Wilk

Reputation: 8113

If you want to convert ',' into '.' on key up, why do you want to handle it with 'specialkey' event? Why don't you use 'keyup' event?

Here's an example:

Ext.create ('Ext.window.Window', {
    renderTo: Ext.getBody () ,
    width: 300 ,
    height: 150 ,
    title: 'It works!' ,
    items: [{
        xtype: 'numberfield' ,
        enableKeyEvents: true ,
        fieldLabel: 'Age' ,
        listeners: {
            keyup: function (nf, evt) {
                // 188 is ','
                if (evt.getKey () == 188) {
                    nf.setRawValue (nf.getRawValue () + '.');
                }
            }
        }
    }]
}).show ();

Otherwise you can use disableKeyFilter property to allow the user to insert special chars, like ,.&*% etc. and then find a way out to replace ',' with '.'

By the way, if you want to handle ',' with specialkey event, here's a trick:

Ext.create ('Ext.window.Window', {
    renderTo: Ext.getBody () ,
    width: 300 ,
    height: 150 ,
    title: 'It works!' ,
    items: [{
        xtype: 'numberfield' ,
        enableKeyEvents: true ,
        fieldLabel: 'Age' ,
        listeners: {
            keyup: function (nf, evt) {
                // 188 is ','
                if (evt.getKey () == 188) {
                    // Fire 'specialkey' up passing ','
                    nf.fireEvent ('specialkey', nf, evt);
                }
            } ,
            specialkey: function (nf, evt) {
                // Here we are!
                alert (evt.getKey ());
            }
        }
    }]
}).show ();

Upvotes: 1

Related Questions