having fun with code

Keyboard shortcuts in Drupal: Navigating through comments

It was one of the best features of Google Reader and Google GMail: the keyboard shortcuts, specially the ones to navigate through messages or posts (n = next, p = previous).

I decided it was time to implement it on spaniards.es, so here is the code I used. So far it only navigates through comments back and forth on the same page (does not go to next page or so). I am planning to add other shortcuts soon.

  1. $(document).keyup(function(event) {
  2.  
  3.   if (event.target.tagName == "INPUT" ||
  4.       event.target.tagName == "TEXTAREA")
  5.     return;
  6.  
  7.   var selected = $('.comment-selected');
  8.  
  9.   // Next comment
  10.   if (event.keyCode == 78) {
  11.     if (selected.length) {
  12.       selected.removeClass('comment-selected');
  13.       selected = selected.nextAll('.comment:first');
  14.       selected.addClass('comment-selected');
  15.     } else {
  16.       selected = $('.comment:first').addClass('comment-selected');
  17.     }
  18.     if (selected.length) {
  19.       window.scrollTo(0, selected.offset().top);
  20.     }
  21.     return;
  22.   }
  23.  
  24.   // Previous comment
  25.   if (event.keyCode == 80) {
  26.     if (selected.length) {
  27.       selected.removeClass('comment-selected');
  28.       selected = selected.prevAll('.comment:first');
  29.       selected.addClass('comment-selected');
  30.     } else {
  31.       selected = $('.comment:last').addClass('comment-selected');
  32.     }
  33.     if (selected.length) {
  34.       window.scrollTo(0, selected.offset().top);
  35.     }
  36.     return;
  37.   }
  38. });

As you can see, it is important to leave the input and textarea fields alone, so the keyboard shortcuts don’t interfere with the user typing some text. With a little bit of CSS the selected comment can be displayed with a different border color or background. And that’s it!

Check an example topic at www.spaniards.es

Related Posts:

2 Comments to Keyboard shortcuts in Drupal: Navigating through comments

  1. December 16, 2008 at 07:16 | Permalink

    Shorcuts are great in general, but I think is a good practice to inform the users about that feature and let them disable shorcuts if they want to.

    The problem on the internet is that each website behaves it’s own way, and although ‘p’ and ‘n’ are great choosen characters for that, if the users don’t know about it, they will never use them (I think that’s why in GMail a small line appears on the bottom of the page explaining some shorcuts and a “learn more” button).

    BTW, shortcuts work great at spaniards ;)

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation