9

I use jQuery-hotkeys

And the following code:

$(document).bind('keydown', 'ctrl+s', function(){$('#save').click()});

but I cannot disable the browser's default behavior. How do I disable it?

jonsca
  • 10,218
  • 26
  • 54
  • 62
zjm1126
  • 34,604
  • 53
  • 121
  • 166

2 Answers2

9

It looks like you return false from your handler to disable "bubbling up" the event. So:

$(document).bind('keydown', 'ctrl+s', function(){$('#save').click(); return false;});

... but it may be browser specific. From your link:

Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as Ctrl-t for new tab, or Ctrl-a for selecting all text. You can always bubble them up to the browser by returning true in your handler.

Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed. Or (Opera/Safari) will not pass those events to the DOM at all.

So, if you bind Ctrl-Q or Alt-F4 and your Safari/Opera window is closed don't be surprised.

James Kolpack
  • 9,331
  • 2
  • 44
  • 59
2

this works in FF too:

$(document).bind('keydown keypress', 'ctrl+s', function(){
  $('#save').click(); 
  return false;
});
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
hujec
  • 21
  • 1