1
0

mousetrap-global-bind.js 987 B

123456789101112131415161718192021222324252627282930313233343536
  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. Mousetrap = (function(Mousetrap) {
  11. var _globalCallbacks = {},
  12. _originalStopCallback = Mousetrap.stopCallback;
  13. Mousetrap.stopCallback = function(e, element, combo, sequence) {
  14. if (_globalCallbacks[combo] || _globalCallbacks[sequence]) {
  15. return false;
  16. }
  17. return _originalStopCallback(e, element, combo);
  18. };
  19. Mousetrap.bindGlobal = function(keys, callback, action) {
  20. Mousetrap.bind(keys, callback, action);
  21. if (keys instanceof Array) {
  22. for (var i = 0; i < keys.length; i++) {
  23. _globalCallbacks[keys[i]] = true;
  24. }
  25. return;
  26. }
  27. _globalCallbacks[keys] = true;
  28. };
  29. return Mousetrap;
  30. }) (Mousetrap);