Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / docs / source / KeyMap.html
1 <html>\r
2 <head>\r
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    \r
4   <title>The source code</title>\r
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
7 </head>\r
8 <body  onload="prettyPrint();">\r
9     <pre class="prettyprint lang-js"><div id="cls-Ext.KeyMap"></div>/**\r
10  * @class Ext.KeyMap\r
11  * Handles mapping keys to actions for an element. One key map can be used for multiple actions.\r
12  * The constructor accepts the same config object as defined by {@link #addBinding}.\r
13  * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key\r
14  * combination it will call the function with this signature (if the match is a multi-key\r
15  * combination the callback will still be called only once): (String key, Ext.EventObject e)\r
16  * A KeyMap can also handle a string representation of keys.<br />\r
17  * Usage:\r
18  <pre><code>\r
19 // map one key by key code\r
20 var map = new Ext.KeyMap("my-element", {\r
21     key: 13, // or Ext.EventObject.ENTER\r
22     fn: myHandler,\r
23     scope: myObject\r
24 });\r
25 \r
26 // map multiple keys to one action by string\r
27 var map = new Ext.KeyMap("my-element", {\r
28     key: "a\r\n\t",\r
29     fn: myHandler,\r
30     scope: myObject\r
31 });\r
32 \r
33 // map multiple keys to multiple actions by strings and array of codes\r
34 var map = new Ext.KeyMap("my-element", [\r
35     {\r
36         key: [10,13],\r
37         fn: function(){ alert("Return was pressed"); }\r
38     }, {\r
39         key: "abc",\r
40         fn: function(){ alert('a, b or c was pressed'); }\r
41     }, {\r
42         key: "\t",\r
43         ctrl:true,\r
44         shift:true,\r
45         fn: function(){ alert('Control + shift + tab was pressed.'); }\r
46     }\r
47 ]);\r
48 </code></pre>\r
49  * <b>Note: A KeyMap starts enabled</b>\r
50  * @constructor\r
51  * @param {Mixed} el The element to bind to\r
52  * @param {Object} config The config (see {@link #addBinding})\r
53  * @param {String} eventName (optional) The event to bind to (defaults to "keydown")\r
54  */\r
55 Ext.KeyMap = function(el, config, eventName){\r
56     this.el  = Ext.get(el);\r
57     this.eventName = eventName || "keydown";\r
58     this.bindings = [];\r
59     if(config){\r
60         this.addBinding(config);\r
61     }\r
62     this.enable();\r
63 };\r
64 \r
65 Ext.KeyMap.prototype = {\r
66     <div id="prop-Ext.KeyMap-stopEvent"></div>/**\r
67      * True to stop the event from bubbling and prevent the default browser action if the\r
68      * key was handled by the KeyMap (defaults to false)\r
69      * @type Boolean\r
70      */\r
71     stopEvent : false,\r
72 \r
73     <div id="method-Ext.KeyMap-addBinding"></div>/**\r
74      * Add a new binding to this KeyMap. The following config object properties are supported:\r
75      * <pre>\r
76 Property    Type             Description\r
77 ----------  ---------------  ----------------------------------------------------------------------\r
78 key         String/Array     A single keycode or an array of keycodes to handle\r
79 shift       Boolean          True to handle key only when shift is pressed, False to handle the key only when shift is not pressed (defaults to undefined)\r
80 ctrl        Boolean          True to handle key only when ctrl is pressed, False to handle the key only when ctrl is not pressed (defaults to undefined)\r
81 alt         Boolean          True to handle key only when alt is pressed, False to handle the key only when alt is not pressed (defaults to undefined)\r
82 handler     Function         The function to call when KeyMap finds the expected key combination\r
83 fn          Function         Alias of handler (for backwards-compatibility)\r
84 scope       Object           The scope of the callback function\r
85 stopEvent   Boolean          True to stop the event from bubbling and prevent the default browser action if the key was handled by the KeyMap (defaults to false)\r
86 </pre>\r
87      *\r
88      * Usage:\r
89      * <pre><code>\r
90 // Create a KeyMap\r
91 var map = new Ext.KeyMap(document, {\r
92     key: Ext.EventObject.ENTER,\r
93     fn: handleKey,\r
94     scope: this\r
95 });\r
96 \r
97 //Add a new binding to the existing KeyMap later\r
98 map.addBinding({\r
99     key: 'abc',\r
100     shift: true,\r
101     fn: handleKey,\r
102     scope: this\r
103 });\r
104 </code></pre>\r
105      * @param {Object/Array} config A single KeyMap config or an array of configs\r
106      */\r
107         addBinding : function(config){\r
108         if(Ext.isArray(config)){\r
109             Ext.each(config, function(c){\r
110                 this.addBinding(c);\r
111             }, this);\r
112             return;\r
113         }\r
114         var keyCode = config.key,\r
115             fn = config.fn || config.handler,\r
116             scope = config.scope;\r
117 \r
118         if (config.stopEvent) {\r
119             this.stopEvent = config.stopEvent;    \r
120         }       \r
121 \r
122         if(typeof keyCode == "string"){\r
123             var ks = [];\r
124             var keyString = keyCode.toUpperCase();\r
125             for(var j = 0, len = keyString.length; j < len; j++){\r
126                 ks.push(keyString.charCodeAt(j));\r
127             }\r
128             keyCode = ks;\r
129         }\r
130         var keyArray = Ext.isArray(keyCode);\r
131         \r
132         var handler = function(e){\r
133             if(this.checkModifiers(config, e)){\r
134                 var k = e.getKey();\r
135                 if(keyArray){\r
136                     for(var i = 0, len = keyCode.length; i < len; i++){\r
137                         if(keyCode[i] == k){\r
138                           if(this.stopEvent){\r
139                               e.stopEvent();\r
140                           }\r
141                           fn.call(scope || window, k, e);\r
142                           return;\r
143                         }\r
144                     }\r
145                 }else{\r
146                     if(k == keyCode){\r
147                         if(this.stopEvent){\r
148                            e.stopEvent();\r
149                         }\r
150                         fn.call(scope || window, k, e);\r
151                     }\r
152                 }\r
153             }\r
154         };\r
155         this.bindings.push(handler);\r
156         },\r
157     \r
158     // private\r
159     checkModifiers: function(config, e){\r
160         var val, key, keys = ['shift', 'ctrl', 'alt'];\r
161         for (var i = 0, len = keys.length; i < len; ++i){\r
162             key = keys[i];\r
163             val = config[key];\r
164             if(!(val === undefined || (val === e[key + 'Key']))){\r
165                 return false;\r
166             }\r
167         }\r
168         return true;\r
169     },\r
170 \r
171     <div id="method-Ext.KeyMap-on"></div>/**\r
172      * Shorthand for adding a single key listener\r
173      * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the\r
174      * following options:\r
175      * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}\r
176      * @param {Function} fn The function to call\r
177      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.\r
178      */\r
179     on : function(key, fn, scope){\r
180         var keyCode, shift, ctrl, alt;\r
181         if(typeof key == "object" && !Ext.isArray(key)){\r
182             keyCode = key.key;\r
183             shift = key.shift;\r
184             ctrl = key.ctrl;\r
185             alt = key.alt;\r
186         }else{\r
187             keyCode = key;\r
188         }\r
189         this.addBinding({\r
190             key: keyCode,\r
191             shift: shift,\r
192             ctrl: ctrl,\r
193             alt: alt,\r
194             fn: fn,\r
195             scope: scope\r
196         });\r
197     },\r
198 \r
199     // private\r
200     handleKeyDown : function(e){\r
201             if(this.enabled){ //just in case\r
202             var b = this.bindings;\r
203             for(var i = 0, len = b.length; i < len; i++){\r
204                 b[i].call(this, e);\r
205             }\r
206             }\r
207         },\r
208 \r
209         <div id="method-Ext.KeyMap-isEnabled"></div>/**\r
210          * Returns true if this KeyMap is enabled\r
211          * @return {Boolean}\r
212          */\r
213         isEnabled : function(){\r
214             return this.enabled;\r
215         },\r
216 \r
217         <div id="method-Ext.KeyMap-enable"></div>/**\r
218          * Enables this KeyMap\r
219          */\r
220         enable: function(){\r
221                 if(!this.enabled){\r
222                     this.el.on(this.eventName, this.handleKeyDown, this);\r
223                     this.enabled = true;\r
224                 }\r
225         },\r
226 \r
227         <div id="method-Ext.KeyMap-disable"></div>/**\r
228          * Disable this KeyMap\r
229          */\r
230         disable: function(){\r
231                 if(this.enabled){\r
232                     this.el.removeListener(this.eventName, this.handleKeyDown, this);\r
233                     this.enabled = false;\r
234                 }\r
235         },\r
236     \r
237     <div id="method-Ext.KeyMap-setDisabled"></div>/**\r
238      * Convenience function for setting disabled/enabled by boolean.\r
239      * @param {Boolean} disabled\r
240      */\r
241     setDisabled : function(disabled){\r
242         this[disabled ? "disable" : "enable"]();\r
243     }\r
244 };</pre>    \r
245 </body>\r
246 </html>