how can i track arrow keys in Chrome and IE?

后端 未结 7 1297
后悔当初
后悔当初 2020-12-06 01:26

Im using foloowing code to track key events

oEvent=window.event || oEvent;
    iKeyCode=oEvent.keyCode || oEvent.which;alert(iKeyCode);

its

7条回答
  •  旧巷少年郎
    2020-12-06 01:43

    problem: webkit sends a keycode 38 (up arrow) to keypress when you do shift-7 (ampersand).

    summary: & and up both equal 38. % and left both equal 37. ' and right both equal 39. ( and down both equal 40. for firefox and opera, the 37/38/39/40 aren't sent for &, %, ', (. they only send those for up, down, left, right. but webkit and ie send 37/38/39/40 for both arrow keys and &, %, ', (. note that for up/down/left/right, webkit sets charCode to 0, but not for &, %, ', (.

    solution: so if you handling those events by keycode, you need to either ignore when the shift key is down or check if the charCode is 0

    • Best Resource: http://unixpapa.com/js/key.html
    • Test a browser: http://asquare.net/javascript/tests/KeyCode.html
    • bugs.jquery.com/ticket/7300. "This is done to match IE. keypress events are only supposed to fire for keys that insert characters. Note that keydown/keyup do fire for arrow keys."
    • Interesting project: github.com/OscarGodson/jKey

提交回复
热议问题