-
-
Save zach2825/5e158942c41afa7df347 to your computer and use it in GitHub Desktop.
;(function ( $, window, document, undefined ) { | |
var pluginName = "keypressAction", | |
defaults = {}; | |
// The actual plugin constructor | |
function keypressAction ( element, options ) { | |
this.element = element; | |
this.settings = $.extend( {}, defaults, options ); | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
$.extend(keypressAction.prototype, { | |
bindKeyToRoute: function (key, route) { | |
Mousetrap.bind([key], function(e) { | |
window.location = route; | |
return false; | |
}); | |
}, | |
init: function () { | |
var self = this; | |
$.each(this.settings.actions, function( index, object ) { | |
self.bindKeyToRoute(object.key, object.route); | |
}); | |
} | |
}); | |
$.fn[ pluginName ] = function ( options ) { | |
this.each(function() { | |
if ( !$.data( this, "plugin_" + pluginName ) ) { | |
$.data( this, "plugin_" + pluginName, new keypressAction( this, options ) ); | |
} | |
}); | |
// chain jQuery functions | |
return this; | |
}; | |
})( jQuery, window, document ); |
good question i have no looked at this in so long, I'm not sure. Sorry I'm no help. I imaging you could extend this and use something like lodash debounce to detect a second set of shortcut keys after the first one has been met
good question i have no looked at this in so long, I'm not sure. Sorry I'm no help. I imaging you could extend this and use something like lodash debounce to detect a second set of shortcut keys after the first one has been met
Thank for quick response, i see that in combination with mousetrap it is possible, so i only add a global ALT+
for all shortcuts here and get the result that i want!
// ... before
bindKeyToRoute: function (key, route) {
Mousetrap.bind([key], function(e) {
// ... after (for have `ALT + key` combination)
bindKeyToRoute: function (key, route) {
Mousetrap.bind(['alt+'+key], function(e) {
// ...
Same thing work also if we define directly in keypressAction() cause it pass key like a string
// without modifing script, pass `ALT + key` directly for keypressAction binding
$(document).keypressAction({
actions: [{
key: 'alt+b',
route: "...my/url..."
}]
});
That's it! Thank you for sharing. Just a note, if your binding html elements you can use the accesskey attribute for buttons and inputs I think.
https://www.w3schools.com/tags/att_global_accesskey.asp
How to do a double key pressAction in example:
ALT + C
orCTRL + B
etc...?