I'm trying to set up a menu that can be navigated via the arrow keys. I have this working fin in Firefox.
Trying to get it to work in IE8 and after a bit of struggle, found that it was because IE8 wouldn't register a keypress on the arrows. To test:
$(document).keypress(function (eh){
alert(eh.keyCode);
};
In Firefox, pressing any of the arrow keys would trigger an alert of 37, 38, 39 or 40.
In IE8, nothing. Any other key on the standard QWERTY keyboard would register, but not the arrow keys.
Is this an issue with my Javascript? A browser setting? A Windows setting?
From the jQuery
keypress
documentation (the last paragraph of the introductory text):It even literally mentions about the arrow keys ;) Thus, you really need to hook on either the
keydown
orkeyup
events. Here's an SSCCE withkeydown
, just copy'n'paste'n'run it. No, you don't need to do a browser-compatible check on theevent
, this works in all browsers from IE6 up to with Firefox.Try this:
Because I sometimes do not want events to bubble for some keys, I use something like: Customize the codes/keys as you see fit.
Use the 'keydown' event