代码之家  ›  专栏  ›  技术社区  ›  harunB10

如果Caps Lock打开,则ExtJS显示消息

  •  3
  • harunB10  · 技术社区  · 6 年前

    如果用户在输入密码时打开caps锁,我想添加一条消息。这是我到目前为止试过的。

     {
         xtype:'textfield',
         itemId: 'field_password_login',
         fieldLabel: 'Password',
         inputType: 'password',
         allowBlank: false,
         listeners:
         {
             keypress: function(tf, e)
             {
                 if (e.getKey() != 13 && e.getKey() != 10 && e.getKey() != 127)
                 {
                     if ((!e.shiftKey && (e.getKey() >= 65 && e.getKey() <= 90)) || ((e.getKey() >= 97 && e.getKey() <= 122) && e.shiftKey))
                     {
                         Ext.getCmp("app_idCAPSIndicator").setText("CAPS LOCK is ON");
                         Ext.getDom("app_idCAPSIndicator").style.color = "navy";
    
                     }
                     else
                     {
                         Ext.getCmp("app_idCAPSIndicator").setText("");
                     }
                 }
                 if (e.getKey() == 13)
                 {
                     Ext.Msg.alert("Enter Pressed");
                 }
             }
         }
     },
     {
         xtype: 'label',
         fieldLabel: '',
         labelWidth: 90,
         labelAlign: 'left',
         labelSeperator: '',
         id: 'app_idCAPSIndicator'
     }
    

    但它不起作用。我没有收到错误消息来知道发生了什么。我在这里做错什么了?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Moataz Sanad    6 年前

    添加enable key events:true,此值为true以启用HTML输入字段的键事件代理

    {
         xtype:'textfield',
         itemId: 'field_password_login',
         fieldLabel: 'Password',
         inputType: 'password',
         allowBlank: false,
         enableKeyEvents: true,
         listeners:
         {
             keypress: function(tf, e)
             {
                 if (e.getKey() != 13 && e.getKey() != 10 && e.getKey() != 127)
                 {
                     if ((!e.shiftKey && (e.getKey() >= 65 && e.getKey() <= 90)) || ((e.getKey() >= 97 && e.getKey() <= 122) && e.shiftKey))
                     {
                         Ext.getCmp("app_idCAPSIndicator").setText("CAPS LOCK is ON");
                         Ext.getDom("app_idCAPSIndicator").style.color = "navy";
    
                     }
                     else
                     {
                         Ext.getCmp("app_idCAPSIndicator").setText("");
                     }
                 }
                 if (e.getKey() == 13)
                 {
                     Ext.Msg.alert("Enter Pressed");
                 }
             }
         }
     },
    
        2
  •  0
  •   Diego Victor de Jesus    6 年前

    这个 Ext.event.Event 有许多类型的键的常量,包括caps lock。另外,这个案例有一个特定的事件处理程序,它是 特色菜 . 所以,我们来看看莫塔兹的回答:

    {
         xtype:'textfield',
         itemId: 'field_password_login',
         fieldLabel: 'Password',
         inputType: 'password',
         allowBlank: false,
         enableKeyEvents: true,
         listeners:
         {
             specialkey: function(tf, e)
             {
                     if (e.getKey() == Ext.event.Event.CAPS_LOCK)
                     {
                         Ext.getCmp("app_idCAPSIndicator").setText("CAPS LOCK is ON");
                         Ext.getDom("app_idCAPSIndicator").style.color = "navy";
                     }
             }
         }
     }
    

    为了提高可读性,应该使用常量而不是数字。这就是Ext.event.event具有键常量的原因之一。

    ExtJS documentation 定义被视为特殊的键。