views:

50

answers:

2

Hello! I'm trying to add some keyboard support for a website I'm working on and I found these plugins for jQuery: shortKeys and jquery.hotkeys (can't post link because i'm a new user). The thing is I want to make it so that when a user presses "j", for example, to go to a different page, "about.html", for example, but, I don't know how to make that happen. Any suggestions?

+2  A: 

You can use window.location in conjunction with either plugin, like this for shortKeys:

$(document).shortkeys({
 'J': function () { window.location = 'about.html'; },
 'K': function () { window.location = 'somethingElse.html'; }
});

Or, using Hotkeys:

$(document).bind('keydown', 'j', function() {
  window.location = 'about.html';
});
Nick Craver
Thank you! That helped a lot!
Victor Ionescu
@Victor - welcome :) be sure to accept answers if they resolve your question to close these out :)
Nick Craver
A: 

You don't need any jquery plugin for this purpose, following piece of code should suffice:

$( document ).keydown(function(event)
{
  switch(event.which)
  {
    case 74: // 74 is keycode for j
      window.location = 'somewhere.html';
      break;

    case 75: // 75 is keycode for k
      window.location = 'another.html';
      break;
  }
});

You can find keycodes of all keys here http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx

ovais.tariq