开发者

How to Trigger an event with keys(ctrl +)(jQuery)

How can I trigger an event w开发者_如何学Pythonith jQuery if I press the Ctrl key plus the ++ key(zoom in).

key = Ctrl ++


Try this

$(window).keypress(function(e){

   if((e.which == 61 && e.ctrlKey) || (e.which == 43 && e.ctrlKey)){
       //Ctrl + "+" is pressed, 61 is for =/+ anr 43 is for Numpad + key
   } 

});


An example of binding to Ctrl+I. Note that you can't override default browser behavior, so many of the Ctrl+(letter) shortcuts are reserved (Ctrl+T = new tab, Ctrl+N = new Window, Ctrl+P = Print etc...)

$(window).keydown(function(e){
  if(e.which == 17)
      $(window).bind('keydown.ctrlI', function(e){
          if(e.which == 73){
              e.preventDefault();
              alert('CTRL+I');
          }
      });
});

$(window).keyup(function(e){
  if(e.which == 17)
      $(window).unbind('keydown.ctrlI');
});


// the element at which you are firing the event
var div = $('#foo');

// the event handler
div.bind('paint', function() {
    $(this).addClass('painted');
});

$(window).keydown(function(e) {
    // if CTRL + + was pressed
    if ( e.ctrlKey && e.which === 187 ) {
        // trigger the event
        div.trigger('paint');
    }
});

Live demo: http://jsfiddle.net/NMYJW/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜