Search code examples
hotkeys

What's the easiest way to add hotkeys to a webpage?


I want to add some hotkeys to my webpage, such as using j/k as page up/down, as this web page did: http://www.theatlantic.com/infocus/2011/07/tour-de-france-2011---part-1/100105/

What's the easiest/cleanest way to do that?


Solution

  • $("#target").keypress(function(event) {
      if ( event.which == 13 //could be any key you want ) {
         // do what you want
       }
    });
    

    This is using the JQuery library (which I think you should use if possible :) )