Custom events and Key bindings = Match made in heaven?
On the back of my example enjoying the Observer pattern with custom events I have started to play with a pet project also involving custom events.
I love keyboard shortcuts. I hate the mouse. I wish that Web applications would offer more keyboard shortcuts a la Gmail, and wondered if there could be a generic way to tie keys to actions in an app. There are things such as accessKey, but we need more.
If you start to follow the pattern of creating named events for public integration points, then how about tieing in keys? I implemented this on the quote example, where you can now use up and down arrows, and N and P, to move through the list of names.
To use the system you declare the keys and tie them to events:
KeyBindings.add({ eventname: 'action:move-up', // keys: KeyBindings.caseInsensitive('p'), keys: ['p', Key.ARROW_UP ], description: "Move up the stack" }); KeyBindings.add({ eventname: 'action:move-down', keys: ['n', Key.ARROW_DOWN ], description: "Move down the stack" });
This code ties the keys to the actions, and thus fires those actions when pressed. Next, you need to capture those events to do the work when the key is fired:
document.observe('action:move-up', function(e) { Selection.moveUp(); }); document.observe('action:move-down', function(e) { Selection.moveDown(); });
With a standardized way of annotating events, interesting side effects appear. You can hit the ‘?’ key to bring down a heads up display sharing what keys do. You could imagine a Greasemonkey script, or browser plugin, that loads the keybindings.js code, and looks for the key binding definitions. The declaration could be done in HTML too, which could be found by the plugin and tied into the system. What do you think?
August 14th, 2008 at 8:55 am
Ahmen! I’m particularly happy with VI controls working in Google Reader… SOOO much faster than clicking. I’ve just submitted a feature request to our developers to add the same VI keyboard shortcuts for browsing the Appcelerator forum postings. Not necessarily intuitive for non-developers (or non-unix people for that matter), but a nice touch nonetheless!
August 14th, 2008 at 9:28 am
What do I think? I think it’s a dirty hack, especially the GreaseMonkey bit. A dirty, delicious hack, that must be explored, so we can arrive at some truly useful patterns of a similar nature. Hack on.
August 14th, 2008 at 9:48 am
Matthew,
Nice. I totally agree. It is a subtle power user touch, that makes a huge different to you if you are using the application/site all the time!
August 14th, 2008 at 9:49 am
Patrick,
I love being called a dirty hack. Very nice indeed, thank you!
August 17th, 2008 at 6:13 pm
Hi!
I’m using your library, thank you so much
How do you think is the better way to control Ctrl/Command + C & P to allow cut/paste in your web?
Thanks!
August 20th, 2008 at 5:17 am
Not working in IE7
Up And Down keys not responding.
N and P keys raise “charCode is null or undefined” line 9 in keybindings.js
Regards.
November 30th, 2009 at 4:51 am
I’ve been saying this should exist forever, but I wouldnt know how to make it, so I’m glad I’ve found it, hope it catches on!