18

我有一个检测最大长度的功能。但问题是当最大长度达到 Ctrl+A 组合不起作用。如何通过 javascript 检测 Ctrl+A 组合。

这是我的最大长度代码。

if (event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 ){
        return true;
} else {            
        if((t.length)>=50) {    
            return false;
        }   
}
4

5 回答 5

32

检查event.ctrlKey

function keyHandler(event) {
    event = event || window.event;
    if(event.keyCode==65 && event.ctrlKey) {
        // ctrl+a was typed.
    }
}
于 2012-11-24T09:11:10.807 回答
2

关键代码:

shift   16
ctrl    17
alt     18

你的jQuery:

$(document).keydown(function (e) {
    if (e.keyCode == 18) {
        alert("ALT was pressed");
    }
});

JavaScript 疯狂:键盘事件

于 2012-11-24T08:56:00.387 回答
1

您可以使用以下内容:

document.onkeypress = function(evt) {
  evt = evt || window.event;
  etv = evt;
  switch (etv.keyCode) {
    case 16:
      // Code to do when Shift presed
      console.log('Pressed [SHIFT]');
      break;
    case 17:
      // Code to do when CTRL presed
      console.log('Pressed [CTRL]');
      break;
    case 32:
      // Code to do when ALT presed
      console.log('Pressed [ALT]');
      break;
  }
};

于 2012-11-24T10:44:40.037 回答
0

我也需要一个解决方案,所以找到了一些有用的东西,将其清理为更少的代码,以及 ES6 ... JSFiddle 链接

function isCapsLock(event=window.event) {
  const code = event.charCode || event.keyCode;

  if (code > 64 && code < 91 && !event.shiftKey) {
    return true;
  }

  return false;
}

document.getElementById("text").addEventListener("keypress", event => {
  const status = document.getElementById("status");
  if (isCapsLock(event)) {
    status.innerHTML = "CapsLocks enabled";
    status.style.color = "red";
  } else {
    status.innerHTML = "CapsLocks disabled";
    status.style.color = "blue";
  }
}, false);
<input type="text" id="text" /><br>
<span id="status"></span>

于 2018-02-22T18:47:52.217 回答
0

这是一个非常古老的问题。gilly3 的答案只有在我们手头有一个 KeyboardEvent 类型的事件对象作为函数参数传递时才有效。如果我们没有可用的事件对象(例如此函数),如何检测当前的控制键状态?

function testModifierKey() {
  // have I some modifier key hold down at this running time?
}

在从spikebrehm的https://gist.github.com/spikebrehm/3747378进行长时间搜索后,我找到了解决方案。他的解决方案是使用带有全局变量的 jQuery 随时跟踪修饰键状态。

全局变量window.modifierKey可以在任何情况下使用,而不需要事件对象。

function testModifierKey() {
  // have I have some modifier key hold down at this executing time?
  if(window.modifierKey) {
    console.log("Some modifier key among shift, ctrl, alt key is currently down.");
    // do something at this condition... for example, delete item without confirmation.
  } else {
    console.log("No modifier key is currently down.");
    // do something at other condition... for example, delete this item from shopping cart with confirmation.
  }
}

这是他要加载到您的 HTML 文档中的脚本:

// source: https://gist.github.com/spikebrehm/3747378
// modifierKey used to check if cmd+click, shift+click, etc. 
!function($, global){
  var $doc = $(document);
  var keys;

  global.modifierKey = false;

   global.keys = keys = {
      'UP': 38,
      'DOWN': 40,
      'LEFT': 37,
      'RIGHT': 39,
      'RETURN': 13,
      'ESCAPE': 27,
      'BACKSPACE': 8,
      'SPACE': 32
  };

  // borrowed from Galleria.js
  var keyboard = {
    map: {},
    bound: false,

    press: function(e) {
      var key = e.keyCode || e.which;
      if ( key in keyboard.map && typeof keyboard.map[key] === 'function' ) {
        keyboard.map[key].call(self, e);
      }
    },

    attach: function(map){
      var key, up;

      for(key in map) {
        if (map.hasOwnProperty(key)) {
          up = key.toUpperCase();
          if (up in keyboard.keys) {
            keyboard.map[keyboard.keys[up]] = map[key];
          } else {
            keyboard.map[up] = map[key];
          }
        }
      }
      if (!keyboard.bound) {
        keyboard.bound = true;
        $doc.bind('keydown', keyboard.press);
      }
    },

    detach: function() {
      keyboard.bound = false;
      keyboard.map = {};
      $doc.unbind('keydown', keyboard.press);
    }
  };

  $doc.keydown(function(e) {
    var key = e.keyCode || e.which;
    if (key === 16 || key === 91 || key === 18 || key === 17) {
      modifierKey = true;
    } else {
      modifierKey = false;
    }
  });

  $doc.keyup(function(e) {
    modifierKey = false;
  });
}(jQuery, window);
于 2020-03-15T16:26:28.183 回答