How can I detect shift + key down in javascript?
JavascriptJavascript Problem Overview
> Possible Duplicate:
> How to catch enter keypress on textarea but not shift+enter?
How can I detect shift + key down in JavaScript?
Javascript Solutions
Solution 1 - 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.
Solution 2 - Javascript
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;
}
}
});