Question

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?

Was it helpful?

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.

OTHER TIPS

for those who need it or something similar, this worked well for me:

    var shift = false;

    $(window).keydown(function(e){
        if(e.shiftKey){
    shift = true;
        }
    })
    .keyup(function(e){
        if(!e.shiftKey){
            shift = false;
        }
    });

    $("#div")
    .mousedown(function(e){
       if(!shift){
         e.stopImmediatePropagation();
         return false;          
       }
    })
    .selectable();
$(window).keydown(function(e){
  if(!e.shiftKey){
     $("#div").selectable({
        start: function(st) {
        st.stopPropagation();
          //your code here
        });

     }
});

if that doesn't work try use document instead of window or 'body'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top