Library to normalize key codes across browsers. This works with keydown events; keypress events are not fired for all keys, and the codes are different for them. It returns an object with the following fields: { int code, bool shift, bool alt, bool ctrl }. The normalized keycodes obey the following rules:
For alphabetic characters, the ASCII code of the uppercase version
For codes that are identical across all browsers (this includes all modifiers, esc, delete, arrows, etc.), the common keycode
For numeric keypad keys, the value returned by numkey(). (Usually 96 + the number)
For symbols, the ASCII code of the character that appears when shift is not held down, EXCEPT for '" => 222 (conflicts with right-arrow/pagedown), .> => 190 (conflicts with Delete) and `~ => 126 (conflicts with Num0).
Basic usage: document.onkeydown = function(e) { do_something_with(KeyCode.translateEvent(e) };
The naming conventions for functions use 'code' to represent an integer keycode, 'key' to represent a key description (specified above), and 'e' to represent an event object.
There's also functionality to track and detect which keys are currently being held down: install 'key_up' and 'key_down' on their respective event handlers, and then check with 'is_down'.
Generates a function key code from a number between 1 and 12
Generates a numeric keypad code from a number between 0 and 9. Also works for (some) arithmetic operators. The mappings are:
*: 106, /: 111, .: 110
+ and - are not supported because the keycodes generated by Mozilla conflict with the non-keypad codes. The same applies to all the arithmetic keypad keys on Konqueror and early Opera.
Generates a key code from the ASCII code of (the first character of) a string.
Checks if two key objects are equal.
Translates a keycode to its normalized value.
Translates a keyDown event to a normalized key event object. The object has the following fields: { int code; boolean shift, boolean alt, boolean ctrl }
Keydown event listener to update internal state of which keys are currently pressed.
Keyup event listener to update internal state.
Returns true if the key spec (as returned by translate_event) is currently held down.
Returns a string representation of a key event suitable for the shortcut.js or JQuery HotKeys plugins. Also makes a decent UI display.