How to determine which key is pressed in javascript?
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
}
in the function which takes the key event:
function(e){
var key = String.fromCharCode(e.keyCode);
}
In JavaScript:
function myKeyPress(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}else
if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
alert(String.fromCharCode(keynum));
alert(keynum);
if (keynum == 13) {
alert("You Pressed Enter Key");
}
}
In HTML:
<textarea name="box" id="box" onkeypress="return myKeyPress(event)"></textarea>
You can handle the keydown
and keyup
events on the document
and set a flag for each key.