78

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

How can I detect shift + key down in JavaScript?

Community
  • 1
  • 1
Bart
  • 4,830
  • 16
  • 48
  • 68
  • 7
    You could've used those extra words to tell everyone what you have tried already. This answers your question, by the way: http://stackoverflow.com/questions/6178431/how-to-catch-enter-keypress-on-textarea-but-not-shiftenter – Pablo Sep 20 '11 at 02:18
  • 2
    oh well... for what it's worth: http://jsfiddle.net/3M6Xt/ – Joseph Marikle Sep 20 '11 at 02:23

2 Answers2

203

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 23
    I think this one is more informative than the selected answer, as it actually describes the behavior of the proper functionality, whereas the OA just presents a bunch of browser hacks for making sure this functionality works. – hoffmanc Oct 26 '12 at 12:08
  • people should use event.key or event.code, beware of deprecation – Akin Hwan Mar 12 '19 at 18:58
  • what exactly is deprecated @AkinHwan can you give us reference!? – Jeton Thaçi Sep 18 '20 at 10:35
47
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;
    }
  }
});
MusikAnimal
  • 2,286
  • 2
  • 22
  • 28
Adam Eberlin
  • 14,005
  • 5
  • 37
  • 49