... but please don't, because you probably shouldn't. I wish my user agent wouldn't respect keyboard shortcuts except perhaps a whitelist of sites. Too often they replace actual browser interaction shortcuts, e.g. spacebar to pagedown (google is often guilty of this).
This is actually a really good point, I've seen many sites take the approach of adding keyboard shortcuts but avoiding the main "default" actions, (eg. space bar, up, down).
I wish the article would go more into keypress events in general and properly organizing code to make attaching shortcuts to it simple, rather than building yet another antipattern into the Jenga tower of bad jQuery code.
The real root of the problem here is systematically tying UI actions to events on specific elements in the DOM. Instead of working around it, why not write slightly cleaner code in the first place?
It's also not strictly necessary to use a third-party library for this unless you're butting heads with one of the edge cases the library is designed to solve.
You could trivially restructure the code to factor out global page actions into an object, like this:
// Make a global page actions object
PageActions = {
selectAll: function (event, context) {...},
nextPage: function (event, context) {...},
prevPage: function (event, context) {...},
};
// Hook up clicks to global actions
// Eg: <a href="#" data-action="selectAll">All</a>
$("[data-action]").click(function () {
var action = $(this).data("action");
PageActions.selectAll("click", this);
});
// Hook up keypresses to global actions
Mousetrap.bind("a", function () {
PageActions.selectAll("keypress", this);
});
This would help with code structure and organization, at the cost of flexibility.
Came here to say this. The author chooses a poor solution upfront and spends the rest of the article kludgeing around conceptual flaws. I love that keyboard shortcuts are getting some attention though, Gmail and Asana are so much nicer for it.
[+] [-] nfoz|12 years ago|reply
[+] [-] foobar2k|12 years ago|reply
[+] [-] fjhqjv|12 years ago|reply
The real root of the problem here is systematically tying UI actions to events on specific elements in the DOM. Instead of working around it, why not write slightly cleaner code in the first place?
It's also not strictly necessary to use a third-party library for this unless you're butting heads with one of the edge cases the library is designed to solve.
[+] [-] foobar2k|12 years ago|reply
[+] [-] rileyjshaw|12 years ago|reply