function checkEnter(event) {
var charcode;
if (event && event.which) {
charcode = event.which;
alert("Case 1. event.which is " + charcode);
}
else if (event && !event.which) {
charcode = event.keyCode;
alert("Case 2. event.keyCode is " + charcode);
}
document.getElementById("text1").value="";
}
<input type="text" id="text1" onkeyup="checkEnter(event)" />
The above function works on both IE7 and Chrome.
function checkKeyPressed() {
document.onkeydown = function(event) {
var charcode;
if (event && event.which) {
charcode = event.which;
alert("charcode is " + charcode);
}
else if (event && !event.which) {
charcode = event.keyCode;
alert("charcode (keyCode) is " + charcode);
}
}
}
<input type="button" id="button1" onclick="checkKeyPressed(event)" value="Button" />
However this one works only in Chrome. Any idea why?