tags:

views:

64

answers:

4

How to determine which key is pressed in javascript?

A: 

You can handle the keydown and keyup events on the document and set a flag for each key.

SLaks
A: 

in the function which takes the key event:

function(e){
   var key = String.fromCharCode(e.keyCode);
}
mkoryak
+2  A: 

Honestly: http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=How+to+determine+which+key+is+pressed+in+javascript%3F

See what I did there?

Matt
@Matt I'll give you a -1 for sarcasm. And a +1 for being right. =P
Aaron Harun
+1  A: 

In jQuery:

jQuery(window).live('keydown', function(e) { 
  var keyCode = e.keyCode || e.which; 

  //do stuff with keycode
  } 
});

Normal:

document.onkeyup = KeyCheck;       

function KeyCheck(){

   var KeyID = event.keyCode;
   //do stuff
}
Aaron Harun