Mousetrap: Keyboard shortcuts in Javascript
craig.is
craig.is
Dealing with keyboard-shortcuts and international keyboard is hard, I don't think I've ever gotten an app using ctrl-[ to work right.
edit: $ highlights when I type shift-4, while that's actually supposed to produce €
(which,keyCode,charCode)
chrome firefox
[ 91 91 91 91 0 91
alt-[ ? 91 0 91
ctrl-[ 27 27 27 91 0 91
esc ? 0 27 0
It's one example out of many, but how do you distinguish such cases, short of enumerating the behavior of every browser (and I guess keyboard layout too)?[1] https://developer.mozilla.org/en/DOM/KeyboardEvent#Virtual_k...
[2] I don't know what chrome does, I couldn't convince it to capture an 'esc' or 'alt-['.
even the URL!
craig.is/killing/mice
Also affects the "* a" combo if you release shift before releasing 8 then release 8 and press "a" it doesn't recognize the combination (presumably because its reading a key press of 8 in between them)
In which case it should be a pretty painless fix.
The way I've seen it done it via a timeout on "release after a long press". If the key has been down for more than ~1 second, then hold off on interpreting a release event to see if there's a matching press already queued in the pipe or not. The timeout can be very short (the X server sends them both synchronously down the socket with no delay).
Also, +1 for a reset() command. I recently patched radio.js to do this, and I think it's something many Ajax-style apps need. However, I'm seeing there might be a need to whitelist some bindings as being durable (survive a reset).
Your website isn't so special to completely change how I use my browser.
It might be okay for a game, but in every other case, if my regular bindings don't work, I leave.
For most websites, I will leave. But for web "apps" like Google Docs, or GMail, or my company's internal apps, I want to use application-style keyboard shortcuts like Ctrl+S and Ctrl+T and Ctrl+N. I want Ctrl+N to create a new document inside my application, not a new browser window.
The best solution would be to ask users to give permission to a webpage to overwrite native keyboard shortcuts for their own uses.
Great app, project page and effort.
It has legitimate uses though, I'm not saying it shouldn't be used. But please use it only where it makes sense.
The problem isn't that keyboard shortcuts for web apps are inherently bad. The problem is application designers who choose their keyboard shortcuts poorly.
- Lack of "key-sequences" ala Gmail
- Bug where focusing on a page having already pushed down a modifier doesn't tell keymaster that the modifier is pushed. For example if you ctrl+tab to a page and keep the control key pushed, then hit an action key (say "f"), then what you really want is to trigger ctrl+f but keymaster doesn't recognize it because it doesn't see ctrl as having been pressed since it was already held down before the page had focus.
Looks like Mousetrap fixes both of these issues. Thanks a ton!
1: https://chrome.google.com/webstore/detail/logpjaacgmcbpdkdch...
The goal of my plugin isn't to just bind keys but to provide sensible defaults and an easy interface to either override the defaults or create your own, as well as have a built-in mechanism to display a "help" modal window. After all, what good are keyboard shortcuts if you don't know what they are.
Check it out. Would love any feedback
(sorry, Craig, for me-too'ing / "hey look at me too"'ing your post).
It would be nice to see an ongoing project for this, with keyboard shortcuts becoming more popular (in use at Twitter, GitHub, and Google Plus for example). I'm planning to add the functionality on my own app soon and will take a look at this.
If you really do want fine-grained control, for example, so that Cmd works on OS X, but it maps to something else entirely on other platforms, you can still use Ctrl and Cmd.
The use case might be to support multiple sets of key bindings, e.g., default, user specific, app specific, view specific. When you don't need one set anymore you just "reset" that set instead of resetting everything.
I like how there's a working demo right there on the page - it's nothing groundbreaking but it definitely adds a bit more polish.
Something I'm wondering about - would it pay to have some sort of "variable" key binding whose value would differ depending on the user's OS (if this is even possible)? For example, instead of binding both command+k and ctrl+k, I would bind one combination that would work both on Mac and Windows systems. This isn't a deal breaker or major issue by any means, but I'd be interested to know if it can be done.
I realize I could achieve this by making my bound function aware of the scoping, but it would be much nicer to have first class library support (in a library with an already kickass-ly simple API). Any thoughts on this?
Just a quick reminder for those using any library like this; screenreader users (typically blind people) rely heavily on the keyboard to use their web browser. Adding your own shortcuts can really screw them over. It's usually best to make your custom shortcuts a feature that the user opts in to, off by default.
Had I know about this I might have used it rather than bundling it into my views.
Nice work!
No. Just don't. Ever. Unless you are doing a standalone, web based app.