简体   繁体   English

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

[英]ExtJS show message if Caps Lock is on

I want to add a message if user turns on the caps lock while entering password.如果用户在输入密码时打开大写锁定,我想添加一条消息。 This is what I've tried so far.这是我迄今为止尝试过的。

 {
     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'
 }

But it does not work.但它不起作用。 I get no error message to know what is happening.我没有收到错误消息来知道发生了什么。 What am I doing wrong here?我在这里做错了什么?

Add enableKeyEvents: true, this true to enable the proxying of key events for the HTML input field添加 enableKeyEvents: 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");
             }
         }
     }
 },

The Ext.event.Event has constants to many types of keys, including caps lock. Ext.event.Event具有许多类型键的常量,包括大写锁定。 Also, there is a specific event handler for this case, which is specialkey .此外,对于这种情况,还有一个特定的事件处理程序,即specialkey So, expanding on Moataz's answer:因此,扩展 Moataz 的回答:

{
     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";
                 }
         }
     }
 }

You should use constants instead of numbers to improve readability.您应该使用常量而不是数字来提高可读性。 That's one reason why Ext.event.Event has key constants.这就是 Ext.event.Event 具有关键常量的原因之一。

ExtJS documentation defines the keys considered to be special. ExtJS 文档定义了被认为是特殊的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM