Often it is useful to intercept the key pressed within an element of an HTML form, such as a text box. For this purpose you can use JavaScript to extract the code of the key that was pressed. Here is a snippet of code you can use—adapting it to your needs, of course.
function keyPressed(e){
var code;
if(window.event){ //IE
code = e.keyCode;
}
else{ //other browsers
code = e.which;
}
//check, for example, if the Enter key was pressed (code 13)
if(code == 13){
//Enter key pressed
}
else{
//Another key pressed
}
}
Note: keyCode is used by Internet Explorer while which is used by the other browsers.
Then, from within a form, you can call this function. For example:
<input onkeypress="javascript:keyPressed(event);" type="text" />