如何在Javascript中处理ctrl + arrow? [英] How to handle ctrl+arrow in Javascript?

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

问题描述

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

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

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

I've handled keydown event. Now when I hold CTRL key then keydown event is fired once. If I hold an arrow (so that now I'm holding CTRL + 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.

我的结果:

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

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

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

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

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

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

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

press CTRL + letter (eg. S) -- 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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