81

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

How can I detect shift + key down in JavaScript?

2
  • 7
    You could've used those extra words to tell everyone what you have tried already. This answers your question, by the way: stackoverflow.com/questions/6178431/… Commented Sep 20, 2011 at 2:18
  • 2
    oh well... for what it's worth: jsfiddle.net/3M6Xt Commented Sep 20, 2011 at 2:23

2 Answers 2

213

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
people should use event.key or event.code, beware of deprecation
what exactly is deprecated @AkinHwan can you give us reference!?
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; } } }); 

1 Comment

All of this code is not needed (anymore). Please see the other answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.