检测页面加载时的大写锁定状态(或类似)

发布于 2024-10-24 01:41:02 字数 133 浏览 2 评论 0原文

在我的研究中,我发现了如何在按下大写键时检测大写锁定。但是,即使在未触摸按键的情况下,我也想知道大写状态。

例子: alert(ui.Keyboard.capslock) // 将返回 true 或 false;

谢谢!

In my research, I've found how to detect capslock when the caps key is pressed. However, I want to know the caps status even in cases when the key is not touched.

example:
alert(ui.Keyboard.capslock) // would return true or false;

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞陪衬 2024-10-31 01:41:02

不,您无法在页面加载时获取键盘按钮的状态。您必须分析按键的按键代码。这是唯一的办法。

No, you cannot get the state of a keyboard button on page load. You have to analyze a keyCode of a keypress. That is the only way.

满天都是小星星 2024-10-31 01:41:02

我刚刚提出了一种替代方案,它将检测大写锁定的状态并将其存储,以便在按下大写锁定键时可以打开和关闭警告。我只针对 chrome 45+ 和 ie9+ 进行编码,因此如果这是您的计划,可能需要对一般用途进行一些调整。

这是我的 html:

<input type="text" id="pwd">
<p id="caps"></p>

这是 js:

var LOGINPAGE = LOGINPAGE || {};
LOGINPAGE.CAPSdetect = {};
$(function() {
    LOGINPAGE.CAPSdetect.engage();
});

LOGINPAGE.CAPSdetect.isDetected = false;
LOGINPAGE.CAPSdetect.capsOn = false;

LOGINPAGE.CAPSdetect.engage = function() {
    $('#pwd').on('keypress', LOGINPAGE.CAPSdetect.shiftDetect);
    $(window).on('keydown', LOGINPAGE.CAPSdetect.capsDetect);
}

LOGINPAGE.CAPSdetect.shiftDetect = function(event) {
    var caps = $('#caps');
    var which = event.keyCode;
    var shift = event.shiftKey;
    var targ = event.target;
    if ((which >= 65 && which <= 90 && !shift) || (which >= 97 && which <= 122 && shift)) {
        caps.html('CAPS LOCK IS ON').css('color', 'crimson');
        LOGINPAGE.CAPSdetect.isDetected = true;
        LOGINPAGE.CAPSdetect.capsOn = true;
    } else if((which >= 65 && which <= 90 && shift) || (which >= 97 && which <= 122 && !shift)){
        caps.html('');
    }
}

LOGINPAGE.CAPSdetect.capsDetect = function(event) {
    if(event.keyCode === 20 && LOGINPAGE.CAPSdetect.isDetected) {
        LOGINPAGE.CAPSdetect.capsOn = (LOGINPAGE.CAPSdetect.capsOn)? false:true;
        if(LOGINPAGE.CAPSdetect.capsOn) {
            $('#caps').html('CAPS LOCK IS ON');
        } else {
            $('#caps').html('');
        }
    }
}

我使用命名空间来避免 isDetectedcapsOn 的全局变量,因此在一些之前的 LOGINPAGE.CAPSDetect.函数和变量。请参阅 this jsfiddle 了解无命名空间并对其进行测试。

I've just come up with an alternative which will detect the state of caps lock and store it so that if the caps lock key is pressed a warning can be turned on and off. I'm only coding for chrome 45+ and ie9+ so there may need to be some adjustments for general use if that is your plan.

Here's my html:

<input type="text" id="pwd">
<p id="caps"></p>

And here's the js:

var LOGINPAGE = LOGINPAGE || {};
LOGINPAGE.CAPSdetect = {};
$(function() {
    LOGINPAGE.CAPSdetect.engage();
});

LOGINPAGE.CAPSdetect.isDetected = false;
LOGINPAGE.CAPSdetect.capsOn = false;

LOGINPAGE.CAPSdetect.engage = function() {
    $('#pwd').on('keypress', LOGINPAGE.CAPSdetect.shiftDetect);
    $(window).on('keydown', LOGINPAGE.CAPSdetect.capsDetect);
}

LOGINPAGE.CAPSdetect.shiftDetect = function(event) {
    var caps = $('#caps');
    var which = event.keyCode;
    var shift = event.shiftKey;
    var targ = event.target;
    if ((which >= 65 && which <= 90 && !shift) || (which >= 97 && which <= 122 && shift)) {
        caps.html('CAPS LOCK IS ON').css('color', 'crimson');
        LOGINPAGE.CAPSdetect.isDetected = true;
        LOGINPAGE.CAPSdetect.capsOn = true;
    } else if((which >= 65 && which <= 90 && shift) || (which >= 97 && which <= 122 && !shift)){
        caps.html('');
    }
}

LOGINPAGE.CAPSdetect.capsDetect = function(event) {
    if(event.keyCode === 20 && LOGINPAGE.CAPSdetect.isDetected) {
        LOGINPAGE.CAPSdetect.capsOn = (LOGINPAGE.CAPSdetect.capsOn)? false:true;
        if(LOGINPAGE.CAPSdetect.capsOn) {
            $('#caps').html('CAPS LOCK IS ON');
        } else {
            $('#caps').html('');
        }
    }
}

I'm using namespaces to avoid globals for isDetected and capsOn hence the LOGINPAGE.CAPSdetect. before some functions and variables. See this jsfiddle for no namespacing and to test it out.

泡沫很甜 2024-10-31 01:41:02

试试这个代码:

    <script language="Javascript">
function capLock(e){
 kc = e.keyCode?e.keyCode:e.which;
 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk)){
  document.getElementById('divon').style.visibility = 'visible';
   document.getElementById('divoff').style.visibility = 'hidden';
 }else{
  document.getElementById('divon').style.visibility = 'hidden';
   document.getElementById('divoff').style.visibility = 'visible';
}
}
</script>
<input type="text" name="trackcaps" onkeypress="capLock(event)" />
<div id="divon" style="visibility:hidden">Caps Lock is on.</div>
<div id="divoff" style="visibility:hidden">Caps Lock is off.</div>

Try this code:

    <script language="Javascript">
function capLock(e){
 kc = e.keyCode?e.keyCode:e.which;
 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk)){
  document.getElementById('divon').style.visibility = 'visible';
   document.getElementById('divoff').style.visibility = 'hidden';
 }else{
  document.getElementById('divon').style.visibility = 'hidden';
   document.getElementById('divoff').style.visibility = 'visible';
}
}
</script>
<input type="text" name="trackcaps" onkeypress="capLock(event)" />
<div id="divon" style="visibility:hidden">Caps Lock is on.</div>
<div id="divoff" style="visibility:hidden">Caps Lock is off.</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文