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

How do you tell if caps lock is on using JavaScript?(你如何判断大写锁定是否在使用 JavaScript?)
本文介绍了你如何判断大写锁定是否在使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)