Domanda

Nell'istruzione Switch sottostante quando viene premuto il tasto sinistro, si avvicina a sinistra e quando viene premuto il tasto superiore.Come posso fare un caso per la combinazione di spostamento e chiave a sinistra.

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

È stato utile?

Soluzione

Il tasto SHIFT è un modificatore e può essere controllato nell'istruzione Case per il tasto sinistro.

$(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.net/zl9fx/1/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top