Keyboard Next/Prev Navigation

Permalink
How would I go about implementing on keypress activation for the next/prev block? I looked for some examples online on how you can do this and the simplest way seems to be through a javascript script like the following.

document.onkeyup = KeyCheck;       
   function KeyCheck(e)
      {
         var KeyID = (window.event) ? event.keyCode : e.keyCode;
         switch(KeyID)
         {
            case 49:
            window.location = "index.html";
            break;
         }
      }


How could I go about implementing this into the next/prev links? Any help is greatly appreciated.

edit:

This would be more useful I think.

$(window.document).keydown(function(e) {
         var code = e.keyCode;
         if (code == 37 || code == 75) {
            $('#load-prev').click();      
         } else if (code == 39 || code == 74) {
            $('#load-next').click();      
         }
      });

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
This is not something I have played with, so is only a guess.

Start with your second bit of code. Using jQuery it has already sorted out all the browser differences on keyboard events.

Create a custom view for the next/prev block, add a <script> section at the end of it, and put the jQuery in. Adapt the #load selectors to match the id of the next/prev buttons or links. Then on your page insert the next/prev block and use the custom view.
Alexmclovin replied on at Permalink Reply
Got it to work. Slightly differently but it works no less as I couldn't get .click() to work or any other solution that was on google except for this one.

$(document).keydown(function(e) {
     if(e.keyCode == 37 || code == 75){
        window.location = '<?php echo View::url($previousCollection->getCollectionPath())?>';
     }else if(e.keyCode == 39 || code == 74){
         window.location = '<?php echo View::url($nextCollection->getCollectionPath())?>';
     }
});
JohntheFish replied on at Permalink Reply
JohntheFish
Once you are happy that it is a reliable solution, maybe package it up as an add-on.