如何检查文本区域上是否按了Shift + Enter [英] How can I check if Shift+Enter is pressed on a textarea
问题描述
如果要按 Enter ,我要提交表单,如果要按 Shift + Enter ,则停止事件。此回调具有 Ext.EventObject
参数,该参数没有提供任何方法来检查是否按下了shift键。
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.
它有两个方法 .hasModifier
和 .isSpecialKey
。两者都返回 boolean
。无法找到是否已按下shift键。如何跟踪它?
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?
这是我的textarea组件:
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.getKey());
}
}
}
我尝试了 evt.shiftKey
。其未定义。
I tried evt.shiftKey
. Its undefined.
推荐答案
使用keydown和keyup事件标志,可以轻松完成这项任务。
This can be done with little trick with keydown and keyup events a flag.
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;
}
}
}
这篇关于如何检查文本区域上是否按了Shift + Enter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!