如何检查是否在文本区域上按下了 Shift+Enter [英] How can I check if Shift+Enter is pressed on a textarea
问题描述
我想在按下 Enter 时提交表单并在按下 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
.无法确定是否按下了 shiftkey.我如何追踪它?
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屋!