SB2055 SB2055 - 1 month ago 24
Javascript Question

jQuery up key / down key keypress detection not working?

In the following code:

$(document).keypress(function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 40) {
alert("down pressed");
} else if (code == 38) {
alert("up pressed");

I'm trying to detect if the down key or up key is pressed. Why isn't it working?


I'm in chrome


Use keydown instead of keypress, some browsers does not fire keypress when an "special key (like arrows)" are pressed