Life before and after kids: Amazon Recommendations Frustrating User Experiences: Wordpress Media Upload
Aug 14

Custom events and Key bindings = Match made in heaven?

Tech with tags: , Add comments

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?

7 Responses to “Custom events and Key bindings = Match made in heaven?”

  1. Matthew Quinlan Says:

    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!

  2. Patrick Mueller Says:

    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.

  3. dion Says:

    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!

  4. dion Says:

    Patrick,

    I love being called a dirty hack. Very nice indeed, thank you!

  5. Garito Says:

    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!

  6. Jorge Says:

    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.

  7. gnounc Says:

    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!

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'