Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.keycode vs event.which

I fell foul of a Firefox keydown behavior 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.

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

Is there any known reason why FF using keypress in this circumstance? What is the benefit?

Once this was established IE8 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:

event.preventDefault ? event.preventDefault() : event.returnValue = false;

During the search to resolve these issues I have been consistently confused by event.keycode vs event.which. Namely am I doing wrong using a switch statement similar to:

$("#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;
     }


Is the following better, if so why?

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


I would just like to know so that I know which is best to apply.

Many thanks.

like image 367
codepuppy Avatar asked Aug 29 '12 07:08

codepuppy


People also ask

Should I use keyCode or key?

If the pressed key inputs an ASCII alphabetic or numeric character with no modifier key, use a keycode for it. If the pressed key inputs an ASCII alphabetic or numeric character with a Shift key modifier, use a keycode for it.

What can I use instead of event keyCode?

Definition and Usage. Note: The keyCode property is deprecated. Use the key property instead.

What's the difference between event key and event code key event properties?

The keyboard event object has two important properties: key and code properties that allow you to detect which key has been pressed. The key property returns the value of the key pressed while the code represents a physical key on the keyboard.

What is which and keyCode in JavaScript?

JavaScript KeyCodeThe keydown event occurs when the keyboard key is pressed, and it is followed at once by the execution of keypress event. The keyup event is generated when the key is released. Here is a sample program using the keycodes You can view it's source to the the actual coding.


2 Answers

Some browsers use keyCode and others use which. But with jQuery this is normalized so you don't have to think about that. You can just choose the one you prefer.

like image 175
sQVe Avatar answered Oct 19 '22 12:10

sQVe


According to this comment jQuery can be unreliable and this page says:

event.which is undefined in IE<9 on keydown and keyup.

event.keyCode is 0 in Gecko (Seamonkey, Firefox) on keypress for keys that return a character.

event.charCode is only supported on keydown and keyup by Internet Explorer (Mac).

Try it on JSFiddle

like image 39
Cees Timmerman Avatar answered Oct 19 '22 14:10

Cees Timmerman