javascriptjqueryjquery-eventsjquery-ui-selectable

Jquery selectable only with shortcut


I want to let the user select when the shift key is held.

$("#div").selectable({
        start: function(st) {
            $(window).keydown(function(e){
                if(!e.shiftKey){
                    st.stopPropagation();
                }
            });
        });

no?


Solution

  • You can shorten down your code to be much simpler by using the .shiftKey property on the event directly (it's present in the mousedown event too), like this:

    $("#div").mousedown(function(e){
       if(e.shiftKey) return;
       e.stopImmediatePropagation();
       return false;
    }).selectable();
    

    You can test it out here.