How can I detect shift + key down in javascript? [duplicate]
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;
}
}
});
精彩评论