ExtJS4:如何检查在文本区域是否按Shift + Enter [英] ExtJS4: how can i check if Shift+Enter is pressed on a textarea
问题描述
如果输入被按下,我想提交表单,如果 Shift + Enter 被按下,则停止该事件。这个回调有 Ext.EventObject
参数,它不提供任何方式来检查shiftkey是否被按下。
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
。它未定义
推荐答案
这可以用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;
}
}
}
这篇关于ExtJS4:如何检查在文本区域是否按Shift + Enter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!