Is there a jQuery plugin that normalizes key codes cross-browser? Meaning, if you press a certain key, then you are guaranteed to get the same key code for every browser that you test in?
Asked
Active
Viewed 464 times
1
-
4It's called [`event.which`](http://api.jquery.com/event.which/). – gen_Eric Dec 18 '12 at 20:10
2 Answers
10
No, however, it is built into jQuery!
$("element").on("keypress",function(e){
console.log(e.which);
});

Kevin B
- 94,570
- 16
- 163
- 180
3
The which
property of the event object is defined for key and mouse events in most browsers, but not IE < 9. jQuery, however normalizes this support: e.which (Thanks @RocketHazmat for pointing this out)
$(this).keyup(function(e) {
var code = e.which;
...
}

gen_Eric
- 223,194
- 41
- 299
- 337

benedict_w
- 3,543
- 1
- 32
- 49
-
Why do you do that? `The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.` http://api.jquery.com/event.which/ – gen_Eric Dec 18 '12 at 20:12
-
-
-
For ie support see - http://stackoverflow.com/questions/10315260/internet-explorer-showing-e-which-as-undefined – benedict_w Dec 18 '12 at 20:15
-
-
-
@benedict_w: Your example is using jQuery already, so `|| e.keyCode` is completly useless. – gen_Eric Dec 18 '12 at 20:19
-