本文介绍了如何在Javascript中处理ctrl + arrow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试捕捉键盘快捷键时发现了一个问题: +箭头.

I've noticed an problem when trying to catch keyboard shortcut: + an arrow.

我已经处理了按键事件.现在,当我按住键时,就会触发一次keydown事件.如果我按住箭头(因此现在按住 +箭头),则不会触发其他事件.是出于任何原因禁止的吗?我想几年前我已经在Opera中遇到了这个问题,并且在浏览器中有一个选项.

I've handled keydown event. Now when I hold key then keydown event is fired once. If I hold an arrow (so that now I'm holding + an arrow) it doesn't fire another event. Is it forbidden from any reason? I guess I've already encountered this problem in Opera a few years ago and there was an option for it in browser.

我的结果:

  • 按住,按箭头-触发事件,不触发箭头

  • holding , press an arrow -- fires event for and doesn't fire an event for an arrow

同时按下 +箭头-触发一个事件,但仅使用的键代码.

press + an arrow at once -- fires one event but only with keycode of .

按住并按一个字母(例如)-可以正常工作

holding , press a letter (eg. ) -- works as expected

按 +字母(例如)-可以正常工作

press + letter (eg. ) -- works as expected

(Chrome和Firefox中的结果相同.上述行为是否是标准行为?)

(Results are identical in Chrome and Firefox. Is the behaviour described above a standard?)

我正在使用:

  • function OnKeyDown(e) { }
  • e.ctrlKey,例如事件的哪些属性
  • function OnKeyDown(e) { }
  • e.ctrlKey, e.which properties of event

问题是:可能是什么问题?

The question is: what might be the problem?

推荐答案

您应检查 event.ctrlKey 标志为true,如下所示:

You should check if the event.ctrlKey flag is true, something like this:

document.getElementById('element').onkeydown = function (e) { 
  e = e || window.event;
  var keyCode = e.keyCode || e.which,
      arrow = {left: 37, up: 38, right: 39, down: 40 };

  if (e.ctrlKey) {
    switch (keyCode) {
      case arrow.left:
      //... handle Ctrl-LeftArrow
      break;
      //...
    }
  }
};

此处中查看示例.

这篇关于如何在Javascript中处理ctrl + arrow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:31