onkeydown event
The onkeydown event activates when a keyboard key is pressed down while on an element. Â The event can be specified as a JavaScript expression or the name of a JavaScript function. Â
If an expression is used, the JavaScript keyword this refers to the element on which the key was pressed down. Â
If the name of a function is used, the function will receive 2 parameters when called:Â
The browser event object; this object can be used to retrieve the JavaScript key code for the key pressed down using the keyCode property; the preventEvent() API can be used to disallow the key
A reference to the element on which the key was pressed down
Example:
The following will restrict keyboard entry to letters A, B, C, D, and F on a grade field.
onkeydown: gradeKeys
function gradeKeys(event, element) {
 var key = event.keyCode;
 switch (key) {
  case 65: // allow A
  case 66: // allow B
  case 67: // allow C
  case 68: // allow D
  case 70: // allow F
  case 8: // allow backspace
  case 46: // allow delete
  case 37: // allow left arrow
  case 39: // allow right arrow
  case 40: // allow down arrow
  case 38: // allow up arrow
  case 13: // allow enter
   break;
  default: // disallow all other keys
   preventEvent(event);
 }
}