trouble adding a keyListener to textfield

  • // the textfield
    var sname = new Ext.form.TextField({
    id: 'sname',
    fieldLabel: '', labelSeparator: '',
    name: 'sname',
    width:270,
    allowBlank: false
    })

    // could not get this to work
    sname.addKeyListener( 13, submitSearch );

    // alternate solution I found
    sname.on( 'specialkey', function (obj,e) {
    if ( e.getKey() == e.RETURN ) { submitSearch(); }
    });


    Is addKeyListener not available to textfields?

    I've tried:
    * the textfield object itself, (object doesn't support this method ..)
    * the .el property of the texfield (el is null or not an object),
    * Ext.get('sname').addKeyListener ( ditto )
    * Ext.getCmp('sname').addKeyListener ( object doesn't support this method ..)

    with no joy. I realize this seems a bit desperate :D
    but would anyone share the appropriate code epigram with me?

    In general, using the docs to track down the availability of the more common methods
    (eg those in Element) has been hit and miss for me.

    Assuming I'm not just stupid and have missed something obvious,
    why isn't a textfield object an Element (and thus have addKeyListener)?

    thanks,
    --dan


  • A TextField is a Component, not an Element. A Component can be comprised of multiple elements, not just one. Generally there is a primary Element somewhere though, and a getEl() funciton for retreiving it (when it is available).

    For a field, using "specialkey" is the way to go. However, if you really want to use the key listener, you just have to wait until the field is rendered and "el" exists:

    sname.render(...);
    sname.getEl().addKeyListener(...);

    or, probably better but more code:

    sname.on('render', function(){
    sname.getEl().addKeyListener(...);
    });


  • Distinguishing between components and elements
    will help quite a bit as I dig deeper into the docs.

    --dan


  • It may help to know then that the only class that extends from Element is Layer. The rest use composition.







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about trouble adding a keyListener to textfield , Please add it free.