你怎么知道使用JavaScript是否打开大写锁定? [英] How do you tell if caps lock is on using JavaScript?

查看:147
本文介绍了你怎么知道使用JavaScript是否打开大写锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何判断是否使用JavaScript开启大写锁定?

How do you tell if caps lock is on using JavaScript?

但有一点需要注意:我确实谷歌了,我找到的最佳解决方案是附加<每个输入都有code> onkeypress 事件,然后每次检查按下的字母是否为大写,如果是,则检查是否也按住了shift。如果不是,那么必须打开大写锁定。这感觉很脏,只是...... 浪费 - 肯定有比这更好的方法吗?

One caveat though: I did google it and the best solution I could find was to attach an onkeypress event to every input, then check each time if the letter pressed was uppercase, and if it was, then check if shift was also held down. If it wasn't, therefore caps lock must be on. This feels really dirty and just... wasteful - surely there's a better way than this?

推荐答案

发现这个有趣....
你可以尝试一下..

Found this interesting.... You can give it a try..

function isCapslock(e){

    e = (e) ? e : window.event;

    var charCode = false;
    if (e.which) {
        charCode = e.which;
    } else if (e.keyCode) {
        charCode = e.keyCode;
    }

    var shifton = false;
    if (e.shiftKey) {
        shifton = e.shiftKey;
    } else if (e.modifiers) {
        shifton = !!(e.modifiers & 4);
    }

    if (charCode >= 97 && charCode <= 122 && shifton) {
        return true;
    }

    if (charCode >= 65 && charCode <= 90 && !shifton) {
        return true;
    }

    return false;

}

对于国际字符,可以为以下内容添加额外的检查根据需要键。您必须获得您感兴趣的字符的键码范围,可以使用键盘映射数组,该数组将保存您正在寻址的所有有效用例键...

For international characters, additional check can be added for the following keys as needed. You have to get the keycode range for characters you are interested in, may be by using a keymapping array which will hold all the valid use case keys you are addressing...

大写AZ或'Ä','Ö','Ü',
小写aZ或0-9或'ä','ö','ü'

uppercase A-Z or 'Ä', 'Ö', 'Ü', lowercase a-Z or 0-9 or 'ä', 'ö', 'ü'

上述键只是样本表示。

这篇关于你怎么知道使用JavaScript是否打开大写锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆