문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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'

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top