1. <small id='4KBQX'></small><noframes id='4KBQX'>

    2. <tfoot id='4KBQX'></tfoot>

        <bdo id='4KBQX'></bdo><ul id='4KBQX'></ul>

        <i id='4KBQX'><tr id='4KBQX'><dt id='4KBQX'><q id='4KBQX'><span id='4KBQX'><b id='4KBQX'><form id='4KBQX'><ins id='4KBQX'></ins><ul id='4KBQX'></ul><sub id='4KBQX'></sub></form><legend id='4KBQX'></legend><bdo id='4KBQX'><pre id='4KBQX'><center id='4KBQX'></center></pre></bdo></b><th id='4KBQX'></th></span></q></dt></tr></i><div id='4KBQX'><tfoot id='4KBQX'></tfoot><dl id='4KBQX'><fieldset id='4KBQX'></fieldset></dl></div>

      1. <legend id='4KBQX'><style id='4KBQX'><dir id='4KBQX'><q id='4KBQX'></q></dir></style></legend>

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

        时间:2023-09-07

        <i id='mVP5Q'><tr id='mVP5Q'><dt id='mVP5Q'><q id='mVP5Q'><span id='mVP5Q'><b id='mVP5Q'><form id='mVP5Q'><ins id='mVP5Q'></ins><ul id='mVP5Q'></ul><sub id='mVP5Q'></sub></form><legend id='mVP5Q'></legend><bdo id='mVP5Q'><pre id='mVP5Q'><center id='mVP5Q'></center></pre></bdo></b><th id='mVP5Q'></th></span></q></dt></tr></i><div id='mVP5Q'><tfoot id='mVP5Q'></tfoot><dl id='mVP5Q'><fieldset id='mVP5Q'></fieldset></dl></div>

        <small id='mVP5Q'></small><noframes id='mVP5Q'>

            <bdo id='mVP5Q'></bdo><ul id='mVP5Q'></ul>

              1. <legend id='mVP5Q'><style id='mVP5Q'><dir id='mVP5Q'><q id='mVP5Q'></q></dir></style></legend>

                  <tbody id='mVP5Q'></tbody>
              2. <tfoot id='mVP5Q'></tfoot>

                  本文介绍了你如何判断大写锁定是否在使用 JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

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

                  但有一个警告:我用谷歌搜索了它,我能找到的最佳解决方案是将 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?

                  推荐答案

                  你可以试一试.添加了一个工作示例.当焦点放在输入上时,打开大写锁定会使 LED 变为红色,否则变为绿色.(没有在mac/linux上测试过)

                  You can give it a try.. Added a working example. When focus is on input, turning on caps lock makes the led go red otherwise green. (Haven't tested on mac/linux)

                  注意:两个版本都适合我.感谢评论中的建设性意见.

                  NOTE: Both versions are working for me. Thanks for constructive inputs in the comments.

                  旧版本:https://jsbin.com/mahenes/edit?js,output

                  另外,这是一个修改版(有人可以在mac上测试并确认)

                  Also, here is a modified version (can someone test on mac and confirm)

                  新版本:https://jsbin.com/xiconuv/edit?js,output

                  新版本:

                  function isCapslock(e) {
                    const IS_MAC = /Mac/.test(navigator.platform);
                  
                    const charCode = e.charCode;
                    const shiftKey = e.shiftKey;
                  
                    if (charCode >= 97 && charCode <= 122) {
                      capsLock = shiftKey;
                    } else if (charCode >= 65 && charCode <= 90
                      && !(shiftKey && IS_MAC)) {
                      capsLock = !shiftKey;
                    }
                  
                    return capsLock;
                  }
                  

                  旧版本:

                  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...

                  大写 A-Z 或 'Ä'、'Ö'、'Ü'、小写 a-Z 或 0-9 或 'ä'、'ö'、'ü'

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

                  以上键只是示例表示.

                  这篇关于你如何判断大写锁定是否在使用 JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:当用户完成输入而不是按键时运行javascript函数 下一篇:在javascript中检测按键的最简单方法

                  相关文章

                  最新文章

                  1. <small id='AZte8'></small><noframes id='AZte8'>

                    <legend id='AZte8'><style id='AZte8'><dir id='AZte8'><q id='AZte8'></q></dir></style></legend>
                  2. <i id='AZte8'><tr id='AZte8'><dt id='AZte8'><q id='AZte8'><span id='AZte8'><b id='AZte8'><form id='AZte8'><ins id='AZte8'></ins><ul id='AZte8'></ul><sub id='AZte8'></sub></form><legend id='AZte8'></legend><bdo id='AZte8'><pre id='AZte8'><center id='AZte8'></center></pre></bdo></b><th id='AZte8'></th></span></q></dt></tr></i><div id='AZte8'><tfoot id='AZte8'></tfoot><dl id='AZte8'><fieldset id='AZte8'></fieldset></dl></div>
                      <bdo id='AZte8'></bdo><ul id='AZte8'></ul>

                    1. <tfoot id='AZte8'></tfoot>