PDA

View Full Version : check if Shift+Enter is pressed on a textarea



shiplu
24 Aug 2012, 8:07 PM
I want to submit the form if Enter is pressed and stop the event if Shift + Enter is pressed. The callback for this has Ext.EventObject parameter which does not provide any way to check if shiftkey is pressed.
it has two methods .hasModifier and .isSpecialKey. Both returns boolean. There is no way to find if shiftkey is pressed. how do I trace it?
This is my textarea component


{
region : 'center',
margins : '5 0 0 0',
xtype : 'textarea',
name : 'chatmessage',
enableKeyEvents: true,
listeners: {
keydown: function(textfield, evt, eOpts){
console.log(evt);
}
}
}
I tried evt.shiftKey. Its undefined.

sword-it
24 Aug 2012, 11:09 PM
HI!

keyCodes:-
Shift- 16
Enter-13

try this:-


listeners: {
keydown:function(tf,e,opt){
if(e.getKey()=='16'){ // if shift is pressed
this.keyPressed=true;
}
},
keyup: function(tf, evt, eOpts){
if(this.keyPressed && (this.keyPressed==true) && (evt.getKey()=='13')){
alert('shift+enter');
// stop your form to submit here
}
else if(evt.getKey()=='13'){
this.keyPressed=false;
alert('enter');
// submit your form here
}
else{this.keyPressed=false;}
}
}



also, this refers here to your textarea

shiplu
25 Aug 2012, 11:28 AM
Thanks sowrd-it. That was a great help. This is the finished code. Sharing so that someone else coming here from google can get the solution..





listeners : {
keydown : function(tf, e, opt) {
if (e.getKey() == e.SHIFT) {
this.shiftKeyPressed = true; // a flag
return;
}
if (e.getKey() != e.ENTER && (this.shiftKeyPressed == undefined || (this.shiftKeyPressed == false))) {
// Submit form
e.stopEvent();
}
},
keyup : function(tf, e, eOpts) {
if (e.getKey() == e.SHIFT) {
this.shiftKeyPressed = false;
}
}
}

sword-it
29 Aug 2012, 11:36 PM
HI!

also, you may use following


listeners: {
keydown: function(field, e){
if((e.getKey() == 13 && e.shiftKey){
alert('shift + enter')
}
}
}