Created
October 13, 2012 17:24
-
-
Save ccampbell/3885446 to your computer and use it in GitHub Desktop.
Extends mousetrap.js to add global bindings that still work in form fields. Just include this js after mousetrap.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* adds a bindGlobal method to Mousetrap that allows you to | |
* bind specific keyboard shortcuts that will still work | |
* inside a text input field | |
* | |
* usage: | |
* Mousetrap.bindGlobal('ctrl+s', _saveChanges); | |
*/ | |
Mousetrap = (function(Mousetrap) { | |
var _global_callbacks = {}, | |
_original_stop_callback = Mousetrap.stopCallback; | |
Mousetrap.stopCallback = function(e, element, combo) { | |
if (_global_callbacks[combo]) { | |
return false; | |
} | |
return _original_stop_callback(e, element, combo); | |
}; | |
Mousetrap.bindGlobal = function(keys, callback, action) { | |
Mousetrap.bind(keys, callback, action); | |
if (keys instanceof Array) { | |
for (var i = 0; i < keys.length; i++) { | |
_global_callbacks[keys[i]] = true; | |
} | |
return; | |
} | |
_global_callbacks[keys] = true; | |
}; | |
return Mousetrap; | |
}) (Mousetrap); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mousetrap=function(a){var d={},e=a.stopCallback;a.stopCallback=function(b,c,a){return d[a]?!1:e(b,c,a)};a.bindGlobal=function(b,c,e){a.bind(b,c,e);if(b instanceof Array)for(c=0;c<b.length;c++)d[b[c]]=!0;else d[b]=!0};return a}(Mousetrap); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work macOS, chrome latest