Here's an interesting thing. A keypress will yield a slightly different event than a keyup. For example, pressing the 'ö' on a german keyboard will get you
// keypress (removed some noise){altKey:false,charCode:246,code:"Semicolon",// this is ignoring other keyboard layouts!ctrlKey:false,key:"ö",// correctkeyCode:246,// the correct char codelocation:0,metaKey:false,shiftKey:false,type:"keypress",which:246,// the correct char code}// keyup (removed some noise){altKey:false,charCode:0,code:"Semicolon",// this is ignoring other keyboard layouts!ctrlKey:false,key:"ö",// correctkeyCode:192,// not the char codelocation:0,metaKey:false,shiftKey:false,type:"keyup",which:192,// not the char code}
So if you want the code of the character and not of the key, you'll want the keypress, not the keyup event.
Here's an interesting thing. A
keypress
will yield a slightly different event than akeyup
. For example, pressing the 'ö' on a german keyboard will get youSo if you want the code of the character and not of the key, you'll want the keypress, not the keyup event.
Thank you for such wonderful information!
Ah yes correct, I'll get to the keyup in a next one actually for a specific reason, but valid point thanks Alex!
According to MDN, the keypress event is deprecated and should not be used in new websites.
Not yet deprecated, though it might be at some point in the future: w3c.github.io/uievents/#event-type...