File: keybindings.js

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (88 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
 * Javascript library to handle a set of keybindings.
 *
 * The user should include this script, and then call setKeybinding(key,
 * callback) for each keybinding that is desired. This script will take care
 * of listening for keypresses and mapping them to the callback functions, or
 * doing nothing if no callback is set.
 *
 * $Horde: horde/templates/javascript/keybindings.js,v 1.5.4.6 2006/03/24 16:05:24 chuck Exp $
 *
 * See the enclosed file COPYING for license information (LGPL). If you did
 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @package Horde
 */

/**
 * A hash of keybindings.
 *
 * @var _keyMap
 */
var _keyMap = new Array();

/**
 * Set up the keypress listener.
 */
if (typeof document.captureEvents != 'undefined') {
    document.captureEvents(Event.KEYPRESS);
    document.onkeypress = keyHandler;
} else {
    document.onkeydown = keyHandler;
}

/**
 * Sets up a callback for a keycode.
 *
 * @param key       The keycode to trigger on.
 * @param callback  The function to call when "key" is pressed. Should be a
 *                  function. It gets the event and the keycode as parameters,
 *                  so that you can use one function to handle multiple keys if
 *                  you like.
 */
function setKeybinding(key, callback)
{
    if (typeof _keyMap[key] == 'undefined') {
        _keyMap[key] = [];
    }

    if (typeof callback == 'function') {
        _keyMap[key].push(callback);
    } else {
        _keyMap[key].push(new Function('e', 'key', callback + '(e, key);'));
    }
}

/**
 * Invoked by the JavaScript event model when a key is pressed. Gets
 * the keycode (attempts to handle all browsers) and looks to see if
 * we have a handler defined for that key. If so, call it.
 *
 * @param e  The event to handle. Should be a keypress.
 */
function keyHandler(e)
{
    var e = e || window.event;

    if (e.keyCode) {
        key = e.keyCode;
    } else if (e.which) {
        key = e.which;
    }

    /* If there's a handler defined for the key that was pressed, call
     * it, passing in the keycode and the event. */
    if (_keyMap[key]) {
        for (var i = 0; i < _keyMap[key].length; ++i) {
            if (_keyMap[key][i](e, key)) {
                e.returnValue = false;
                if (typeof e.preventDefault == 'function') {
                    e.preventDefault();
                } else {
                    e.returnValue = true;
                }
                break;
            }
        }
    }
}