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?
2 Answers 2
$("#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
slandau
24.2k43 gold badges124 silver badges186 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
}
});