4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-KeyMap'>/**
19 </span> * @class Ext.util.KeyMap
20 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
21 * The constructor accepts the same config object as defined by {@link #addBinding}.
22 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
23 * combination it will call the function with this signature (if the match is a multi-key
24 * combination the callback will still be called only once): (String key, Ext.EventObject e)
25 * A KeyMap can also handle a string representation of keys.<br />
27 <pre><code>
28 // map one key by key code
29 var map = new Ext.util.KeyMap("my-element", {
30 key: 13, // or Ext.EventObject.ENTER
35 // map multiple keys to one action by string
36 var map = new Ext.util.KeyMap("my-element", {
37 key: "a\r\n\t",
42 // map multiple keys to multiple actions by strings and array of codes
43 var map = new Ext.util.KeyMap("my-element", [
46 fn: function(){ alert("Return was pressed"); }
49 fn: function(){ alert('a, b or c was pressed'); }
54 fn: function(){ alert('Control + shift + tab was pressed.'); }
57 </code></pre>
58 * <b>Note: A KeyMap starts enabled</b>
60 Ext.define('Ext.util.KeyMap', {
61 alternateClassName: 'Ext.KeyMap',
63 <span id='Ext-util-KeyMap-method-constructor'> /**
64 </span> * Creates new KeyMap.
65 * @param {Mixed} el The element to bind to
66 * @param {Object} binding The binding (see {@link #addBinding})
67 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
69 constructor: function(el, binding, eventName){
74 eventName: eventName || me.eventName,
78 me.addBinding(binding);
85 <span id='Ext-util-KeyMap-method-addBinding'> /**
86 </span> * Add a new binding to this KeyMap. The following config object properties are supported:
88 Property Type Description
89 ---------- --------------- ----------------------------------------------------------------------
90 key String/Array A single keycode or an array of keycodes to handle
91 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)
92 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)
93 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)
94 handler Function The function to call when KeyMap finds the expected key combination
95 fn Function Alias of handler (for backwards-compatibility)
96 scope Object The scope of the callback function
97 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><code>
103 var map = new Ext.util.KeyMap(document, {
104 key: Ext.EventObject.ENTER,
109 //Add a new binding to the existing KeyMap later
116 </code></pre>
117 * @param {Object/Array} binding A single KeyMap config or an array of configs
119 addBinding : function(binding){
120 if (Ext.isArray(binding)) {
121 Ext.each(binding, this.addBinding, this);
125 var keyCode = binding.key,
133 if (Ext.isString(keyCode)) {
135 keyString = keyCode.toLowerCase();
137 for (i = 0, len = keyString.length; i < len; ++i){
138 keys.push(keyString.charCodeAt(i));
144 if (!Ext.isArray(keyCode)) {
149 for (i = 0, len = keyCode.length; i < len; ++i) {
151 if (Ext.isString(key)) {
152 keyCode[i] = key.toLowerCase().charCodeAt(0);
157 this.bindings.push(Ext.apply({
162 <span id='Ext-util-KeyMap-method-handleKeyDown'> /**
163 </span> * Process any keydown events on the element
165 * @param {Ext.EventObject} event
167 handleKeyDown: function(event) {
168 if (this.enabled) { //just in case
169 var bindings = this.bindings,
171 len = bindings.length;
173 event = this.processEvent(event);
174 for(; i < len; ++i){
175 this.processBinding(bindings[i], event);
180 <span id='Ext-util-KeyMap-method-processEvent'> /**
181 </span> * Ugly hack to allow this class to be tested. Currently WebKit gives
182 * no way to raise a key event properly with both
184 * b) The alt/ctrl/shift modifiers
185 * So we have to simulate them here. Yuk!
186 * This is a stub method intended to be overridden by tests.
187 * More info: https://bugs.webkit.org/show_bug.cgi?id=16735
190 processEvent: function(event){
194 <span id='Ext-util-KeyMap-method-processBinding'> /**
195 </span> * Process a particular binding and fire the handler if necessary.
197 * @param {Object} binding The binding information
198 * @param {Ext.EventObject} event
200 processBinding: function(binding, event){
201 if (this.checkModifiers(binding, event)) {
202 var key = event.getKey(),
203 handler = binding.fn || binding.handler,
204 scope = binding.scope || this,
205 keyCode = binding.keyCode,
206 defaultEventAction = binding.defaultEventAction,
209 keydownEvent = new Ext.EventObjectImpl(event);
212 for (i = 0, len = keyCode.length; i < len; ++i) {
213 if (key === keyCode[i]) {
214 if (handler.call(scope, key, event) !== true && defaultEventAction) {
215 keydownEvent[defaultEventAction]();
223 <span id='Ext-util-KeyMap-method-checkModifiers'> /**
224 </span> * Check if the modifiers on the event match those on the binding
226 * @param {Object} binding
227 * @param {Ext.EventObject} event
228 * @return {Boolean} True if the event matches the binding
230 checkModifiers: function(binding, e){
231 var keys = ['shift', 'ctrl', 'alt'],
236 for (; i < len; ++i){
239 if (!(val === undefined || (val === e[key + 'Key']))) {
246 <span id='Ext-util-KeyMap-method-on'> /**
247 </span> * Shorthand for adding a single key listener
248 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
250 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
251 * @param {Function} fn The function to call
252 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
254 on: function(key, fn, scope) {
255 var keyCode, shift, ctrl, alt;
256 if (Ext.isObject(key) && !Ext.isArray(key)) {
274 <span id='Ext-util-KeyMap-method-isEnabled'> /**
275 </span> * Returns true if this KeyMap is enabled
278 isEnabled : function(){
282 <span id='Ext-util-KeyMap-method-enable'> /**
283 </span> * Enables this KeyMap
287 this.el.on(this.eventName, this.handleKeyDown, this);
292 <span id='Ext-util-KeyMap-method-disable'> /**
293 </span> * Disable this KeyMap
297 this.el.removeListener(this.eventName, this.handleKeyDown, this);
298 this.enabled = false;
302 <span id='Ext-util-KeyMap-method-setDisabled'> /**
303 </span> * Convenience function for setting disabled/enabled by boolean.
304 * @param {Boolean} disabled
306 setDisabled : function(disabled){
314 <span id='Ext-util-KeyMap-method-destroy'> /**
315 </span> * Destroys the KeyMap instance and removes all handlers.
316 * @param {Boolean} removeEl True to also remove the attached element
318 destroy: function(removeEl){
323 if (removeEl === true) {