h__
h__

Reputation: 883

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?

Upvotes: 2

Views: 295

Answers (2)

Martin
Martin

Reputation: 211

I leave a solution that doesn't require jQuery, in case useful:

  document.addEventListener('keydown', function(event) {

    if (event.key === 'j') {
      window.scrollBy(0, 80); // Scroll down by 80 pixels
    }

    if (event.key === 'k') {
      window.scrollBy(0, -80); // Scroll up by 80 pixels
    }

  });

Upvotes: 0

slandau
slandau

Reputation: 24082

$("#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 :) )

Upvotes: 0

Related Questions