Detecting Arrow key press in IE via javascript/jQuery

后端 未结 4 1114
灰色年华
灰色年华 2020-12-16 14:27

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 struggl

4条回答
  •  醉酒成梦
    2020-12-16 14:58

    From the jQuery keypress documentation (the last paragraph of the introductory text):

    Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 97 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

    It even literally mentions about the arrow keys ;) Thus, you really need to hook on either the keydown or keyup events. Here's an SSCCE with keydown, just copy'n'paste'n'run it. No, you don't need to do a browser-compatible check on the event, this works in all browsers from IE6 up to with Firefox.

    
    
        
            SO question 2217553
            
            
        
        
           

    Press one of the arrow keys.

提交回复
热议问题