我如何检查大写锁定是否已打开?

时间:2020-05-06 20:32:56

标签: javascript

我想检查大写锁定是否打开,但是使用我的代码,我只能检查 大写而不是大写锁定。

jQuery('#password').keypress(function(e) {
        var s = String.fromCharCode( e.which );
        if ( s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey ) {
          jQuery('#caps').show();
        }
        else { 
          jQuery('#caps').hide();
        }
      });

这是我的JS代码。

1 个答案:

答案 0 :(得分:0)

您可以使用 event.getModifierState("CapsLock") 进行如下检查:-

var inputVal = document.getElementById("customInput");
var textMsg = document.getElementById("warningDiv");

inputVal.addEventListener("keyup", function(event) {

if (event.getModifierState("CapsLock")) {
    textMsg.style.display = "block";
  } else {
    textMsg.style.display = "none"
  }
});
#warningDiv{
  color: red;
  display: none;
}
<input type="text" id="customInput" />

<p id="warningDiv">WARNING! Caps lock is ON.</p>

您也可以在这里查看我的解决方案- https://codepen.io/Arnab_Datta/pen/gOavXVJ?editors=1111