Created
September 30, 2015 18:07
-
-
Save zach2825/5e158942c41afa7df347 to your computer and use it in GitHub Desktop.
keypressAction.js
This file contains hidden or 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
;(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 ); |
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
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!Same thing work also if we define directly in keypressAction() cause it pass key like a string