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