按住 Shift 键时的 Keydown 事件行为

Keydown event behavior when holding down Shift key

我注意到,当您按住 A 这样的字母键时,只要您按住它,就会连续触发 keydown 事件。但是,当您按住 shift 时,事件只会触发一次。我想将我的应用程序逻辑基于该观察结果,但我不知道这是否是每个浏览器中的标准行为。谁能确认一下?

不幸的是,这不是您可以信赖的标准行为。我对我安装的几个浏览器(IE Edge、IE 11、Chrome 55,都在 windows 机器上)进行了快速测试,其中所有 3 个浏览器都自动重复了两个字母的 keydown 事件键(如 A)和修饰键(如 shift),与你所说的浏览器所做的相反。

Jan Wolter here and Peter-Paul Koch here 所述,在不同的浏览器中一致地处理键盘事件一直很困难。 Wolter 写了关于自动重复的文章:

If a key is held down long enough it typically auto-repeats, and some additional events will be triggered on each autorepeat. On Macintosh and Linux systems, modifier keys usually don't auto-repeat, but on Windows systems they do

然后 Wolter 继续列出各种例外情况。