Module documentation for keycode.js

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'.

Module

Author
Jonathan Tang
Version
0.9
License
BSD

Function Index

fkey
numkey
Generates a numeric keypad code from a number between 0 and 9.
key
Generates a key code from the ASCII code of (the first character of) a string.
key_equals
Checks if two key objects are equal.
translate_key_code
Translates a keycode to its normalized value.
translate_event
Translates a keyDown event to a normalized key event object.
key_down
Keydown event listener to update internal state of which keys are currently pressed.
key_up
Keyup event listener to update internal state.
is_down
Returns true if the key spec (as returned by translate_event) is currently held down.
hot_key
Returns a string representation of a key event suitable for the shortcut.

Functions

fkey

Generates a function key code from a number between 1 and 12

Params
num

numkey

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.

Params
num

key

Generates a key code from the ASCII code of (the first character of) a string.

Params
str

key_equals

Checks if two key objects are equal.

Params
key1
key2

translate_key_code

Translates a keycode to its normalized value.

Params
code

translate_event

Translates a keyDown event to a normalized key event object. The object has the following fields: { int code; boolean shift, boolean alt, boolean ctrl }

Params
e

key_down

Keydown event listener to update internal state of which keys are currently pressed.

Params
e

key_up

Keyup event listener to update internal state.

Params
e

is_down

Returns true if the key spec (as returned by translate_event) is currently held down.

Params
key

hot_key

Returns a string representation of a key event suitable for the shortcut.js or JQuery HotKeys plugins. Also makes a decent UI display.

Params
key