views:

23

answers:

2

Hi, is there a quick way to use the right and left arrow keys to load the next page? My solution so far is:

$(document).ready(function () {
    $("a.transition").click(function (event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(20, redirectPage);

    });
    $("a.transitionB").click(function (event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(20, redirectPage);

    });
    function redirectPage() {
        window.location = linkLocation;
    }
});

with 2 invisible link layers. But i need something to control the transition without clicking in the page, but to use the arrow buttons.

Thank you for your help.

+2  A: 
$(document).keyup(function(e) {
    console.log(e.keyCode);
});

Edit :

$(document).keyup(function(e) {
  switch(e.keyCode) {
    case 37 : alert('You pressed Left'); break;
    case 38 : alert('You pressed Up'); break;
    case 39 : alert('You pressed Right'); break;
    case 40 : alert('You pressed Down'); break;
   }
});

You can give it a try here http://jsbin.com/uzigu4

Ninja Dude
A: 

Try this (different browser == different keycodes):

function redirectPage(href) {
    window.location = href;
}

function onKey(e) {
    var key = e.keyCode ? e.keyCode : e.which;

    if (key == 37 || key == 26) {
        e.preventDefault();
        $("a.transition").click();
    } else if (key == 39 || key == 27) {
        e.preventDefault();
        $("a.transitionB").click();
    }
}

$(document).ready(function () {
    $(document).keypress(onKey);
    $("a.transition, a.transitionB").click(function (event) {
        event.preventDefault();
        $("body").fadeOut(20, function() {redirectPage(this.href)});
    });
});
hluk
you guys are awesome, thank you