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