[ Index ] |
|
Code source de Horde 3.1.3 |
1 /** 2 * Javascript library to handle a set of keybindings. 3 * 4 * The user should include this script, and then call setKeybinding(key, 5 * callback) for each keybinding that is desired. This script will take care 6 * of listening for keypresses and mapping them to the callback functions, or 7 * doing nothing if no callback is set. 8 * 9 * $Horde: horde/templates/javascript/keybindings.js,v 1.5.4.6 2006/03/24 16:05:24 chuck Exp $ 10 * 11 * See the enclosed file COPYING for license information (LGPL). If you did 12 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 13 * 14 * @package Horde 15 */ 16 17 /** 18 * A hash of keybindings. 19 * 20 * @var _keyMap 21 */ 22 var _keyMap = new Array(); 23 24 /** 25 * Set up the keypress listener. 26 */ 27 if (typeof document.captureEvents != 'undefined') { 28 document.captureEvents(Event.KEYPRESS); 29 document.onkeypress = keyHandler; 30 } else { 31 document.onkeydown = keyHandler; 32 } 33 34 /** 35 * Sets up a callback for a keycode. 36 * 37 * @param key The keycode to trigger on. 38 * @param callback The function to call when "key" is pressed. Should be a 39 * function. It gets the event and the keycode as parameters, 40 * so that you can use one function to handle multiple keys if 41 * you like. 42 */ 43 function setKeybinding(key, callback) 44 { 45 if (typeof _keyMap[key] == 'undefined') { 46 _keyMap[key] = []; 47 } 48 49 if (typeof callback == 'function') { 50 _keyMap[key].push(callback); 51 } else { 52 _keyMap[key].push(new Function('e', 'key', callback + '(e, key);')); 53 } 54 } 55 56 /** 57 * Invoked by the JavaScript event model when a key is pressed. Gets 58 * the keycode (attempts to handle all browsers) and looks to see if 59 * we have a handler defined for that key. If so, call it. 60 * 61 * @param e The event to handle. Should be a keypress. 62 */ 63 function keyHandler(e) 64 { 65 var e = e || window.event; 66 67 if (e.keyCode) { 68 key = e.keyCode; 69 } else if (e.which) { 70 key = e.which; 71 } 72 73 /* If there's a handler defined for the key that was pressed, call 74 * it, passing in the keycode and the event. */ 75 if (_keyMap[key]) { 76 for (var i = 0; i < _keyMap[key].length; ++i) { 77 if (_keyMap[key][i](e, key)) { 78 e.returnValue = false; 79 if (typeof e.preventDefault == 'function') { 80 e.preventDefault(); 81 } else { 82 e.returnValue = true; 83 } 84 break; 85 } 86 } 87 } 88 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |