views:

29

answers:

3

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?

A: 
$(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'

Val
the shift key is "shiftKey". what i'm asking is how to limit the user to only being able to select when the shift key is being held.
Luke Burns
see updated answer
Val
A: 

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();
Luke Burns
+1  A: 

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.

Nick Craver