event.keycode v event.which
<p>Good Morning All,</p>

<p>I fell foul of a Fire Fox Keydown behaviour in that pressing the enter key (indeed any key) without having focus on a specific field will NOT trigger a keydown event it will only trigger a Keypress event. </p>

<p>This could be very confusing as the keydown and keyup event use javascripts keycodes whereas keypress uses ASCII codes. Fortunately 13 (enter/return) is common to both.</p>

<p>Is there any known reason why FF using keypress in this circumstance? what is the benefit?</p>

<p>Once this was established I.E 8 threw up a silly in that it does not permit preventDefault demanding instead returnValue = false the following snippet from another SO post has proved very useful:</p>

<pre><code>event.preventDefault ? event.preventDefault() : event.returnValue = false;
</code></pre>

<p>During the search to resolve these issues I have been consistently confused by event.keycode v event.which.  Namely am I doing wrong using a switch statement similar to:-</p>

<pre><code>$("#class_Name").bind("keydown", function(event){
    // do not test input if field controls used
    switch(event.which){
       case 13:
       //enter key 
       event.preventDefault ? event.preventDefault() : event.returnValue = false;
       break;
     }
</code></pre>

<p>Is the following better, if so why?</p>

<pre><code>$("body").keypress(function(event){
     // stop inadvertant form submission
     if (event.keycode == "13"){
       event.preventDefault ? event.preventDefault() : event.returnValue = false;
     }
});
</code></pre>

<p>I would just like to know so that I know which is best to apply.</p>

<p>Many thanks.</p>
