最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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/

发布评论

评论列表(0)

  1. 暂无评论