1
0

mousetrap-global-bind.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * adds a bindGlobal method to Mousetrap that allows you to
  3. * bind specific keyboard shortcuts that will still work
  4. * inside a text input field
  5. *
  6. * usage:
  7. * Mousetrap.bindGlobal('ctrl+s', _saveChanges);
  8. */
  9. /* global Mousetrap:true */
  10. (function(Mousetrap) {
  11. var _globalCallbacks = {};
  12. var _originalStopCallback = Mousetrap.prototype.stopCallback;
  13. Mousetrap.prototype.stopCallback = function(e, element, combo, sequence) {
  14. var self = this;
  15. if (self.paused) {
  16. return true;
  17. }
  18. if (_globalCallbacks[combo] || _globalCallbacks[sequence]) {
  19. return false;
  20. }
  21. return _originalStopCallback.call(self, e, element, combo);
  22. };
  23. Mousetrap.prototype.bindGlobal = function(keys, callback, action) {
  24. var self = this;
  25. self.bind(keys, callback, action);
  26. if (keys instanceof Array) {
  27. for (var i = 0; i < keys.length; i++) {
  28. _globalCallbacks[keys[i]] = true;
  29. }
  30. return;
  31. }
  32. _globalCallbacks[keys] = true;
  33. };
  34. Mousetrap.init();
  35. }) (Mousetrap);