2011-05-12 8 views
2

아래의 switch 문에서 왼쪽 키를 누르면 왼쪽으로 경고하고 위쪽 키를 누르면 맨 위로 경고합니다. shift와 left 키를 조합하여 어떻게 만들 수 있습니까?jQuery e.which in switch 문

$(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 combination of shift + left arrow keys. 
      break; 
    } 
}); 

답변

5

shift 키는 수정 자이며 case 키에서 왼쪽 키를 확인할 수 있습니다.

$(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; 
    } 
}); 

데모 : http://jsfiddle.net/ZL9Fx/1/

+0

감사합니다. 그것은 위대한 작품. 나는 조건부 단축키'e.shiftKey? '를 사용하기 전에 똑같은 것을 시도했다. alert ('shift + left') : alert ('left'); 그리고 나는 또한'e.which == 16? 경고 ('shift + left') : 경고 ('left'); 그리고 작동하지 않았습니다. 이 바로 가기가 작동하지 않는 이유가 있습니까? – Pinkie

+1

@Pinkie,'alert (e.shiftKey? 'shift + left': 'left')' –

+0

@Pinkie : 저에게 맞는 말 : http://jsfiddle.net/ZL9Fx/2/ –