Skip to content

Instantly share code, notes, and snippets.

@zach2825
Created September 30, 2015 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zach2825/5e158942c41afa7df347 to your computer and use it in GitHub Desktop.
Save zach2825/5e158942c41afa7df347 to your computer and use it in GitHub Desktop.
keypressAction.js
;(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 );
@arturmamedov
Copy link

How to do a double key pressAction in example: ALT + C or CTRL + B etc...?

@zach2825
Copy link
Author

zach2825 commented Jul 9, 2019

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

@arturmamedov
Copy link

arturmamedov commented Jul 9, 2019

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..."
    }]
});

@zach2825
Copy link
Author

zach2825 commented Jul 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment