Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / api / Ext.util.KeyMap.html
1 <!DOCTYPE html><html><head><title>Ext.util.KeyMap | Ext JS 4.0 Documentation</title><script type="text/javascript" src="../ext-all.js"></script><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../scrollbars.css" type="text/css"><link rel="stylesheet" href="../docs.css" type="text/css"><link id="styleCss" rel="stylesheet" href="../style.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script><link rel="stylesheet" href="../prettify.css" type="text/css"><!-- link(rel: 'stylesheet', href: req.baseURL + '/css/ext4.css', type: 'text/css')--><link rel="shortcut icon" type="image/ico" href="../favicon.ico"><!--[if IE]>
2 <style type="text/css">.head-band { display: none; }
3 .header { border: 0; top: 0; left: 0px; background: url(../header.gif) repeat-x; }
4 .doc-tab .members .member a.more { background-color: #efefef; }
5 </style><link rel="stylesheet" href="/new/css/ie.css" type="text/css"><![endif]-->
6 </head><body id="ext-body" class="iScroll"><div id="notice" class="notice">For up to date documentation and features, visit 
7 <a href="http://docs.sencha.com/ext-js/4-0">http://docs.sencha.com/ext-js/4-0</a></div><div class="wrapper"><div class="head-band"></div><div class="header"><h2><a href="../index.html">Sencha Documentation</a></h2></div><div id="search"><form><input type="text" placeholder="Search" id="search-field" autocomplete="off" name="q"></form><div id="search-box"></div></div><div id="treePanel"></div><div id="container"><script type="text/javascript">
8
9     req = {
10         liveURL: '.',
11         standAloneMode: true,
12         origDocClass: 'Ext.util.KeyMap',
13         docClass: 'Ext.util.KeyMap',
14         docReq: 'Ext.util.KeyMap',
15         version: '4.0',
16         baseURL: '.',
17         baseDocURL: '.',
18         baseProdURL: '.'
19     };
20
21     clsInfo = {};
22
23
24
25 </script>
26
27 <script type="text/javascript" src="../search.js"></script>
28 <!--script type="text/javascript" src="/new/javascripts/app/examples.js"></script-->
29 <script type="text/javascript" src="../class_tree.js"></script>
30 <script type="text/javascript" src="../class_doc.js"></script>
31 <script type="text/javascript">
32     req.source = 'KeyMap.html#Ext-util.KeyMap';
33     clsInfo = {"methods":["KeyMap","addBinding","destroy","disable","enable","isEnabled","on","setDisabled"],"cfgs":[],"properties":[],"events":[],"subclasses":[]};
34     Ext.onReady(function() {
35         Ext.create('Docs.classPanel');
36     });
37 </script><div id="top-block" class="top-block"><h1 id="clsTitle" class="cls"><a href="../source/KeyMap.html#Ext-util.KeyMap" target="_blank">Ext.util.KeyMap</a></h1></div><div id="docContent"><div id="doc-overview-content"><div class="lft"><p>Handles mapping keys to actions for an element. One key map can be used for multiple actions.
38 The constructor accepts the same config object as defined by <a href="Ext.util.KeyMap.html#addBinding" rel="Ext.util.KeyMap#addBinding" class="docClass">addBinding</a>.
39 If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
40 combination it will call the function with this signature (if the match is a multi-key
41 combination the callback will still be called only once): (String key, <a href="Ext.EventObject.html" rel="Ext.EventObject" class="docClass">Ext.EventObject</a> e)
42 A KeyMap can also handle a string representation of keys.<br />
43 Usage:</p>
44
45 <pre class="prettyprint"><code>// map one key by key code
46 var map = new Ext.util.KeyMap("my-element", {
47     key: 13, // or Ext.EventObject.ENTER
48     fn: myHandler,
49     scope: myObject
50 });
51
52 // map multiple keys to one action by string
53 var map = new Ext.util.KeyMap("my-element", {
54     key: "a\r\n\t",
55     fn: myHandler,
56     scope: myObject
57 });
58
59 // map multiple keys to multiple actions by strings and array of codes
60 var map = new Ext.util.KeyMap("my-element", [
61     {
62         key: [10,13],
63         fn: function(){ alert("Return was pressed"); }
64     }, {
65         key: "abc",
66         fn: function(){ alert('a, b or c was pressed'); }
67     }, {
68         key: "\t",
69         ctrl:true,
70         shift:true,
71         fn: function(){ alert('Control + shift + tab was pressed.'); }
72     }
73 ]);
74 </code></pre>
75
76
77 <p><b>Note: A KeyMap starts enabled</b></p>
78 <div class="members"><div class="m-methods"><a name="methods"></a><div class="definedBy">Defined By</div><h3 class="mth p">Methods</h3><div id="method-KeyMap" class="member f ni"><a href="Ext.util.KeyMap.html#method-KeyMap" rel="method-KeyMap" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-constructor" class="viewSource">view source</a></div><a name="KeyMap"></a><a name="method-KeyMap"></a><a href="Ext.util.KeyMap.html#" rel="method-KeyMap" class="cls expand">KeyMap</a>(
79 <span class="pre">Mixed el, Object binding, [String eventName]</span>)
80  : void</div><div class="description"><div class="short"><p>&nbsp;</p></div><div class="long">
81 <h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Mixed<div class="sub-desc"><p>The element to bind to</p>
82 </div></li><li><span class="pre">binding</span> : Object<div class="sub-desc"><p>The binding (see <a href="Ext.util.KeyMap.html#addBinding" rel="Ext.util.KeyMap#addBinding" class="docClass">addBinding</a>)</p>
83 </div></li><li><span class="pre">eventName</span> : String<div class="sub-desc"><p>(optional) The event to bind to (defaults to "keydown")</p>
84 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
85 </li></ul></div></div></div><div id="method-addBinding" class="member ni"><a href="Ext.util.KeyMap.html#method-addBinding" rel="method-addBinding" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-addBinding" class="viewSource">view source</a></div><a name="addBinding"></a><a name="method-addBinding"></a><a href="Ext.util.KeyMap.html#" rel="method-addBinding" class="cls expand">addBinding</a>(
86 <span class="pre">Object/Array binding</span>)
87  : void</div><div class="description"><div class="short">Add a new binding to this KeyMap. The following config object properties are supported:
88
89 Property            Type    ...</div><div class="long"><p>Add a new binding to this KeyMap. The following config object properties are supported:</p>
90
91 <pre>Property            Type             Description
92 ----------          ---------------  ----------------------------------------------------------------------
93 key                 String/Array     A single keycode or an array of keycodes to handle
94 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)
95 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)
96 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)
97 handler             Function         The function to call when KeyMap finds the expected key combination
98 fn                  Function         Alias of handler (for backwards-compatibility)
99 scope               Object           The scope of the callback function
100 defaultEventAction  String           A default action to apply to the event. Possible values are: stopEvent, stopPropagation, preventDefault. If no value is set no action is performed. 
101 </pre>
102
103
104 <p>Usage:</p>
105
106 <pre><code>// Create a KeyMap
107 var map = new Ext.util.KeyMap(document, {
108     key: Ext.EventObject.ENTER,
109     fn: handleKey,
110     scope: this
111 });
112
113 //Add a new binding to the existing KeyMap later
114 map.addBinding({
115     key: 'abc',
116     shift: true,
117     fn: handleKey,
118     scope: this
119 });
120 </code></pre>
121
122 <h3 class="pa">Parameters</h3><ul><li><span class="pre">binding</span> : Object/Array<div class="sub-desc"><p>A single KeyMap config or an array of configs</p>
123 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
124 </li></ul></div></div></div><div id="method-destroy" class="member ni"><a href="Ext.util.KeyMap.html#method-destroy" rel="method-destroy" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-destroy" class="viewSource">view source</a></div><a name="destroy"></a><a name="method-destroy"></a><a href="Ext.util.KeyMap.html#" rel="method-destroy" class="cls expand">destroy</a>(
125 <span class="pre">Boolean removeEl</span>)
126  : void</div><div class="description"><div class="short"><p>Destroys the KeyMap instance and removes all handlers.</p>
127 </div><div class="long"><p>Destroys the KeyMap instance and removes all handlers.</p>
128 <h3 class="pa">Parameters</h3><ul><li><span class="pre">removeEl</span> : Boolean<div class="sub-desc"><p>True to also remove the attached element</p>
129 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
130 </li></ul></div></div></div><div id="method-disable" class="member ni"><a href="Ext.util.KeyMap.html#method-disable" rel="method-disable" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-disable" class="viewSource">view source</a></div><a name="disable"></a><a name="method-disable"></a><a href="Ext.util.KeyMap.html#" rel="method-disable" class="cls expand">disable</a> : void</div><div class="description"><div class="short"><p>Disable this KeyMap</p>
131 </div><div class="long"><p>Disable this KeyMap</p>
132 <h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
133 </li></ul></div></div></div><div id="method-enable" class="member ni"><a href="Ext.util.KeyMap.html#method-enable" rel="method-enable" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-enable" class="viewSource">view source</a></div><a name="enable"></a><a name="method-enable"></a><a href="Ext.util.KeyMap.html#" rel="method-enable" class="cls expand">enable</a> : void</div><div class="description"><div class="short"><p>Enables this KeyMap</p>
134 </div><div class="long"><p>Enables this KeyMap</p>
135 <h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
136 </li></ul></div></div></div><div id="method-isEnabled" class="member ni"><a href="Ext.util.KeyMap.html#method-isEnabled" rel="method-isEnabled" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-isEnabled" class="viewSource">view source</a></div><a name="isEnabled"></a><a name="method-isEnabled"></a><a href="Ext.util.KeyMap.html#" rel="method-isEnabled" class="cls expand">isEnabled</a> : Boolean</div><div class="description"><div class="short"><p>Returns true if this KeyMap is enabled</p>
137 </div><div class="long"><p>Returns true if this KeyMap is enabled</p>
138 <h3 class="pa">Returns</h3><ul><li><span class="pre">Boolean</span>&nbsp; &nbsp;
139 </li></ul></div></div></div><div id="method-on" class="member ni"><a href="Ext.util.KeyMap.html#method-on" rel="method-on" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-on" class="viewSource">view source</a></div><a name="on"></a><a name="method-on"></a><a href="Ext.util.KeyMap.html#" rel="method-on" class="cls expand">on</a>(
140 <span class="pre">Number/Array/Object key, Function fn, [Object scope]</span>)
141  : void</div><div class="description"><div class="short"><p>Shorthand for adding a single key listener</p>
142 </div><div class="long"><p>Shorthand for adding a single key listener</p>
143 <h3 class="pa">Parameters</h3><ul><li><span class="pre">key</span> : Number/Array/Object<div class="sub-desc"><p>Either the numeric key code, array of key codes or an object with the
144 following options:
145 {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}</p>
146 </div></li><li><span class="pre">fn</span> : Function<div class="sub-desc"><p>The function to call</p>
147 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.</p>
148 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
149 </li></ul></div></div></div><div id="method-setDisabled" class="member ni"><a href="Ext.util.KeyMap.html#method-setDisabled" rel="method-setDisabled" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.util.KeyMap.html" class="definedIn docClass">Ext.util.KeyMap</a><br/><a href="../source/KeyMap.html#Ext-util.KeyMap-method-setDisabled" class="viewSource">view source</a></div><a name="setDisabled"></a><a name="method-setDisabled"></a><a href="Ext.util.KeyMap.html#" rel="method-setDisabled" class="cls expand">setDisabled</a>(
150 <span class="pre">Boolean disabled</span>)
151  : void</div><div class="description"><div class="short"><p>Convenience function for setting disabled/enabled by boolean.</p>
152 </div><div class="long"><p>Convenience function for setting disabled/enabled by boolean.</p>
153 <h3 class="pa">Parameters</h3><ul><li><span class="pre">disabled</span> : Boolean<div class="sub-desc">
154 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
155 </li></ul></div></div></div></div></div></div></div><div id="pageContent"></div></div></div></div></body></html>