Mac中的Capslock不会触发keyDown事件 [英] keyDown event is not fired for Capslock in Mac

查看:248
本文介绍了Mac中的Capslock不会触发keyDown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CHROME(52):

CHROME (52):

当大写锁定开启时 - 仅触发keydown(keyUp或keyPress中没有事件)

When turning caps lock ON - only keydown is fired (no event in keyUp or keyPress)

当关闭大写锁定时 - 仅触发键盘(在keyDown或keyPress中没有事件)

When turning caps lock OFF - only keyup is fired (no event in keyDown or keyPress)

FIREFOX(46):

FIREFOX (46):

只有两个大写锁定ON& OFF(没有keyUp或keyPress)

Only keyDown event is fired for both caps lock ON & OFF (no keyUp or keyPress)

我在这里读到了关于keyCodes和事件的信息 http://www.quirksmode.org/js/keys.html 和MDN在这里 https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode ,aaaand here http://unixpapa.com/js/key.html

I've read about the keyCodes and events here http://www.quirksmode.org/js/keys.html and in MDN here https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode, aaaand here http://unixpapa.com/js/key.html

但上述链接都没有谈到这种奇怪的行为。
这是预期的吗?如果是这样,任何更简单的方法来处理它?<​​/ p>

But none of the above links talks about this weird behaviour. Is this expected? If so, any easier way to handle it?

推荐答案

是的,这是预期的。

Chrome将 CAPS ON 视为 keydown ,因为它将开启/关闭视为按住,就像我们持有 shift 键,打开行为上限并在我们发布时关闭。此大写锁定按钮也是。当您启用大写锁定时,Chrome会将'打开'作为按键处理,当您时'关闭'它将其作为 keyup 处理。但是,firefox处理所有内容为 keydown ,与Chrome处理相同的内容相比,这对我来说没有意义。

Chrome treats the CAPS ON as keydown because it treats the on/off as press and hold, like we hold shift key, which turns on caps on behaviour and turns off when we release it. This Caps Lock button also. When you turn on Caps Lock, chrome handles the 'turn on' as a keypress and when you 'turn off' it handles it as a keyup. But, firefox handles everything as keydown which doesn't make sense to me when compared to how chrome handles the same.

解决方案

您应该使用 getModifierState()获取大写锁定的状态。 chrome和firefox支持此功能。

You should use getModifierState() to get the state of the Caps Lock. This is supported in chrome and firefox.

希望有所帮助!

$(function() {
  $(window).on("keydown", function(e){
    if (e.which === 20)
      console.log(e.originalEvent.getModifierState('CapsLock'))
  });
  $(window).on("keyup", function(e) {
    if (e.which === 20)
      console.log(e.originalEvent.getModifierState('CapsLock'))
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Focus here and press 'Caps Lock'

这篇关于Mac中的Capslock不会触发keyDown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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