window.event.keyCode how to do it on Firefox? [duplicate]

Some browsers have a global event object, other send the event object to the event handler as a parameter. Chrome and Internet Exlporer uses the former, Firefox uses the latter.

Some browsers use keyCode, others use charCode.

Use arguments[0] to pick up the event object sent as a parameter, and if there is no object there, there is a global object instead:

onkeydown="doKey(arguments[0] || window.event)"

In the method you can check for either keyCode or charCode

function doKey(event) {
  var key = event.keyCode || event.charCode;
  ...
}

Note the lowercase name onkeydown. If you are using XHTML event names has to be lowercase, or the browser might ignore it.

Leave a Comment