Results 1 to 5 of 5

Thread: how can I listen to a keyPressed in textfield

  1. #1
    Ext User groth's Avatar
    Join Date
    Sep 2008
    Posts
    90
    Vote Rating
    0
      0  

    Red face how can I listen to a keyPressed in textfield

    Hi,

    I am trying to listen to keypressed on text field and only when the user press tab to do something.

    I did the following:
    Code:
     
    var
    
    street =new Ext.form.TextField({ fieldLabel
    :'Street', name
    :'street', enableKeyEvents
    :true, onKeyPress
    :function(field, key){ if(key == Ext.EventObject.TAB){
    Ext
    .MessageBox.alert('key pressed'); }
    }
    });
    I did this because the TextField API show that :
    keypress : ( Ext.form.TextField this, Ext.EventObject e )
    Keypress input field event. This event only fires if enableKeyEvents is set to true.
    Keypress input field event. This event only fires if enableKeyEvents is set to true. Listeners will be called with the following arguments:
    • this : Ext.form.TextField This text field
    • e : Ext.EventObject

    but apperently the second parameter of the function is not EventObject
    How can I get the key that was pressed?

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    132
      0  

    Default

    The API docs are completely correct, but you are overriding onKeyPress instead of using the listener!

    Code:
    listeners: {
      keypress: function(field, e) {
        if(e.getKey() == Ext.EventObject.TAB){
          Ext.MessageBox.alert('key pressed');
        }
      }
    }

  3. #3
    Ext User groth's Avatar
    Join Date
    Sep 2008
    Posts
    90
    Vote Rating
    0
      0  

    Default

    This indeed help - thanks Condor!

    however I don't understand the logic difference between use the listener and overriding the on<Event>() command.

    But never mind

  4. #4
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    132
      0  

    Default

    Whenever possible use the event and don't override the handler.

    The handler usually not only fires the event, but also does some extra processing, which you would loose if you would override the method.

  5. #5
    Sencha User
    Join Date
    Jan 2013
    Posts
    28
    Vote Rating
    2
      0  

    Default

    Hi , i have a function like this

    Code:
     listeners: {
            keypress: me.submitOnEnter
     }


    where i am doing some logic but i needed trim values form text fields so i override text js file like this

    Code:
    onKeyPress: function (e) {
     // trim logic 
    }
    problem is keypress function is getting executed before my override function so in
    Code:
    submitOnEnter
    function i am not getting trim values .
    Thanks in advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •