Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Js - Obj.keycode Comes Back Undefined


  • Please log in to reply
3 replies to this topic

#1 rolf

rolf

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 675 posts
  • Location:Suffolk UK

Posted 06 May 2009 - 07:07 AM

I'm trying to write a script that redirects the user to certain URL dependent on which key they press (i.e. s takes them to search.php, m takes them to map.php etc)

Having failed to get this to work I'm just trying to get the method straight by getting an alert box to recognise the 'y' key or to tell me the ascii code for any other key pressed, but it's not recognising the 'y' key and keeps coming back with an undefined value. the code I'm using is:

CODE
window.document.onkeydown = keyPress;
function keyPress(e)
{
    var obj = (navigator.appName == "Microsoft Internet Explorer") ? event : e;
    var code = obj.keyCode;
    if(obj.keyCode == 121 ){alert('yes')} else {alert('no '.code);}
}


any help would be much appreciated.

Thanks in advance

#2 NASA

NASA

    HR 4

  • Active Members
  • PipPipPipPip
  • 183 posts

Posted 06 May 2009 - 07:16 AM

try this...
CODE
function doSomething(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    alert('Character was ' + character);
}


#3 rolf

rolf

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 675 posts
  • Location:Suffolk UK

Posted 06 May 2009 - 07:35 AM

Thanks NASA, you are a complete star! I've been banging my head against that for far too long for it to be funny!

Cheers ale.gif

#4 NASA

NASA

    HR 4

  • Active Members
  • PipPipPipPip
  • 183 posts

Posted 06 May 2009 - 08:18 AM

YVMW smile.gif




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

We are now a read-only forum.
 
No new posts or registrations allowed.