2

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?

F_V
3862 gold badges13 silver badges18 bronze badges
asked Jul 13, 2011 at 20:52

2 Answers 2

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

answered Jul 13, 2011 at 20:55
Sign up to request clarification or add additional context in comments.

Comments

0

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
 }
 });
answered Feb 6 at 8:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.