javascript - jQuery e.which in switch statement - Stack Overflow

In the below switch statement when left key is pressed, it alerts left and when top key is pressed it a

In the below switch statement when left key is pressed, it alerts left and when top key is pressed it alerts top. How can i make a case for bination of both shift and left key.

$(document).keydown(function(e) {
    switch (e.which) {
        case 37: alert('left'); //left arrow key
            break;
        case 38: alert('top');; //up arrow key
            break;
        case ??: alert('shift + left'); //How can i make this repond to the bination of shift + left arrow keys.
            break;
    }
});

In the below switch statement when left key is pressed, it alerts left and when top key is pressed it alerts top. How can i make a case for bination of both shift and left key.

$(document).keydown(function(e) {
    switch (e.which) {
        case 37: alert('left'); //left arrow key
            break;
        case 38: alert('top');; //up arrow key
            break;
        case ??: alert('shift + left'); //How can i make this repond to the bination of shift + left arrow keys.
            break;
    }
});
Share Improve this question asked May 12, 2011 at 21:27 PinkiePinkie 10.3k22 gold badges81 silver badges124 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

The shift key is a modifier, and can be checked in the case statement for the left key.

$(document).keydown(function(e) {
    switch (e.which) {
     case 37:
        if (e.shiftKey) {
            alert('shift+left'); // shift and left arrow key
        }
        else {
            alert('left'); //left arrow key
        }
        break;
     case 38:
        alert('top'); //up arrow key
        break;
    }
});

Demo: http://jsfiddle/ZL9Fx/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745571743a4633723.html

相关推荐

  • javascript - jQuery e.which in switch statement - Stack Overflow

    In the below switch statement when left key is pressed, it alerts left and when top key is pressed it a

    21小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信