PDA

View Full Version : How to get new value of TextField on a KeyPress event?



thedott
11 Nov 2010, 5:51 AM
Er, I posted this in the wrong Forum the first time so here I go again...

Is there any way to get the "new" value of a Textfield during a KeyPress event like you can during a KeyUp? As it is implemented, the Textfield.getValue() gets the old value, i.e. the value preceding the field being updated. My goal is to implement on-the-fly string formatting.

Thanks!

Animal
11 Nov 2010, 6:23 AM
myInputElement.on("keyup", function(){console.log(myInputElement.dom.value);})


Works fine for me.

Of course if you paste into it, you're a bit stuffed...

thedott
11 Nov 2010, 6:55 AM
Thanks, the KeyUp event is working like I'm expecting, but it's the KeyPress event that I'm asking about.

What I'm seeing in the KeyPress event is that the textfield.getValue() returns the previous value than the new value that is in the field. (This is not the case for the KeyUp event.) To me, the event appears to be called before the textfield is updated to the new value. Is there a way to update the textfield value prior to calling getValue() so that I get the new value that I entered instead?

Animal
11 Nov 2010, 7:25 AM
Listener option delay: 1 makes it work for me.

thedott
11 Nov 2010, 7:42 AM
Thanks Animal, that does the job :-)

groberts2002
22 Feb 2012, 6:44 AM
Thanks, the delay solved the same problem for me just now.

Greetings from West Bridgford.

vijayt
7 Aug 2012, 12:04 AM
Just curious, how does that delay 1 on keyup is worked as a work around for keypress..

Can we see the code change for that?

I am also looking to handle keypress event in TextField but not the keyup.

sword-it
7 Aug 2012, 12:16 AM
hi vijayt

check this


{
xtype: 'textfield'


, enableKeyEvents: true

, listeners:{

keypress: function(field){

console.log(field.getValue());

}

}

}

Regards

vijayt
7 Aug 2012, 1:09 AM
Thanks a lot!!!!

But my problem is, I am using sencha touch... In sencha touch only keyup is triggering not the keypress event on Textfield.

ASV Perm
13 Aug 2014, 10:09 PM
I followed your thread, but finally resulted in another solution: to call keydown then keyup events. onKeyDown() records its current textfield value (before edit), then onKeyUp records its current textfield value (after edit) and calls other method passing newValue and oldValue as parameters (like onChange).