开发者

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

开发者 https://www.devze.com 2023-04-06 04:58 出处:网络
This question already has answers here: 开发者_Python百科 Closed 11 years ago. Possible Duplicate: How to catch enter keypress on textarea but not shift+enter?
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

精彩评论

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