Javascript 如果Caps Lock处于启用状态,则ExtJS显示消息

Javascript 如果Caps Lock处于启用状态,则ExtJS显示消息,javascript,extjs,Javascript,Extjs,如果用户在输入密码时打开大写锁定,我想添加一条消息。这就是我到目前为止所尝试的 { xtype:'textfield', itemId: 'field_password_login', fieldLabel: 'Password', inputType: 'password', allowBlank: false, listeners: { keypress: function(tf, e)

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

 {
     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'
 }
{
xtype:'textfield',
itemId:'字段\密码\登录',
fieldLabel:“密码”,
输入类型:“密码”,
allowBlank:false,
听众:
{
按键:功能(tf,e)
{
如果(e.getKey()!=13和&e.getKey()!=10和&e.getKey()!=127)
{

如果(!e.shiftKey&&(e.getKey()>=65&&e.getKey()=97&&e.getKey()添加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");
             }
         }
     }
 },
{
xtype:'textfield',
itemId:'字段\密码\登录',
fieldLabel:“密码”,
输入类型:“密码”,
allowBlank:false,
enableKeyEvents:true,
听众:
{
按键:功能(tf,e)
{
如果(e.getKey()!=13和&e.getKey()!=10和&e.getKey()!=127)
{
如果(!e.shiftKey&&(e.getKey()>=65&&e.getKey()=97&&e.getKey()对许多类型的键都有常量,包括caps lock。此外,对于这种情况,还有一个特定的事件处理程序,它是specialkey。因此,扩展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";
                 }
         }
     }
 }
您应该使用常量而不是数字来提高可读性。这就是Ext.event.event具有键常量的原因之一


定义被认为是特殊的键。

它可以工作,但我必须打开caps lock并开始键入以显示消息。当caps lock打开时,我如何设置警报?如果不键入…如果将侦听器更改为keyup,则可以检测caps lock的按下,keypress没有检测到caps lock键我尝试使用的
keyup
它可以检测到不起作用。现在,无论大写锁定是打开还是关闭,只要我开始写入,大写锁定就会打开。我使用:
侦听器:{specialkey:function(field,e){if(e.getKey()==20&&!toolTip.isVisible(){toolTip.show();}else{toolTip.hide();}}},