开发者

How can I detect shift + key down in javascript? [duplicate]

This question already has answers here: 开发者_Python百科 Closed 11 years ago.

Possible Duplicate:

How to catch enter keypress on textarea but not shift+enter?

How can I detect shift + key down in JavaScript?


event.shiftKey is a boolean. true if the Shift key is being pressed, false if not. altKey and ctrlKey work the same way.

So basically you just need to detect the keydown as normal with onkeydown, and check those properties as needed.


var onkeydown = (function (ev) {
  var key;
  var isShift;
  if (window.event) {
    key = window.event.keyCode;
    isShift = !!window.event.shiftKey; // typecast to boolean
  } else {
    key = ev.which;
    isShift = !!ev.shiftKey;
  }
  if ( isShift ) {
    switch (key) {
      case 16: // ignore shift key
        break;
      default:
        alert(key);
        // do stuff here?
        break;
    }
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜