Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keypress and keyup - why is the keyCode different?

Related: JavaScript KeyCode vs CharCode

Here is some code you can try at home or in a jsfiddle:

el.addEventListener( 'keyup', function( e ) {      console.log( 'Keyup event' );     console.log( e.keyCode ); } );  el.addEventListener( 'keypress', function( e ) {      console.log( 'Keypress event' );     console.log( e.keyCode ); } ); 

Why is the keyCode different?

I can understand why one should use keypress only, but what I don't understand is how two key events, given the same hit key on the keyboard, give different keyCodes.

PS: I'm not worrying about legacy browsers support, I tried this in Chrome and was surprised, and couldn't find an explanation.

like image 826
Florian Margaine Avatar asked Jun 14 '12 09:06

Florian Margaine


People also ask

What is the difference between Keyup and keypress?

keypress Fires when an actual character is being inserted in, for instance, a text input. It repeats while the user keeps the key depressed. keyup Fires when the user releases a key, after the default action of that key has been performed.

What is the difference between a keypress and Keydown and a Keyup event?

keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key. keyup – fires when any key is released, fires last, and the browser processes the key.

What is the difference between the Keydown and Keyup events?

The keydown event occurs when the key is pressed, followed immediately by the keypress event. Then the keyup event is generated when the key is released.

What is the difference between keypress and Keydown?

The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.


2 Answers

The events are for completely different purposes. Use keyup and keydown for identifying physical keys and keypress for identifying typed characters. The two are fundamentally different tasks with different events; don't try to mix the two. In particular, keyCode on keypress events is usually redundant and shouldn't be used (except in older IE, but see the linked document below for more on that); for printable keypresses it's usually the same as which and charCode, although there is some variation between browsers.

Jan Wolter's article on key events, already linked to in another answer, is the definitive word on this subject for me and has tables describing what each of the different properties returns for each type of key event and each browser.

like image 50
Tim Down Avatar answered Sep 21 '22 15:09

Tim Down


There is a good article on quirksmode.org answering exactly that question. You might also want to look at Unixpapa's results.

like image 27
Bergi Avatar answered Sep 19 '22 15:09

Bergi