0

i want to ask how can i change the web page when the arrow key pushed by user , like a web of manga/book if we want to next page we just push the arrow key

sorry for my english , thanks before

Brice
  • 1,026
  • 9
  • 20
Mamen
  • 1,322
  • 5
  • 21
  • 44

3 Answers3

1

You can use jQuery for that:

$(document).keydown(function(e) {
    if (e.which == 37) { 
       alert("left");
       e.preventDefault();
       return false;
    }

    if (e.which == 39) { 
       alert("right");
       e.preventDefault();
       return false;
    }
});
Legionar
  • 7,472
  • 2
  • 41
  • 70
  • Nice answer. Note to OP, this is leveraging the jQuery framework and will not work without. Legionar, can you provide a plain javascript example in addition for the +1, unless OP adds the jQuery tag. – David Houde Nov 07 '13 at 11:54
1

If you wish to use the jQuery framework, then look at Binding arrow keys in JS/jQuery

In plain javascript, I'll go with :

document.onkeydown = function (e) { 
  e = e || window.event; 
  var charCode = (e.charCode) ? e.charCode : e.keyCode;

  if (charCode == 37) {
        alert('left');
  }
  else if (charCode == 39) {
    alert('right');
  }
};
Community
  • 1
  • 1
Brice
  • 1,026
  • 9
  • 20
1

Here is a non jQuery option:

document.onkeydown = arrowChecker;

function arrowChecker(e) {  
    e = e || window.event;
    if (e.keyCode == '37') { //left
        document.location.href = "http://stackoverflow.com/";
    }
    else if (e.keyCode == '39') { //right
       document.location.href = "http://google.com/";
    }
}
joao
  • 292
  • 2
  • 7