3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.2
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.KeyMap"></div>/**
17 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
18 * The constructor accepts the same config object as defined by {@link #addBinding}.
19 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
20 * combination it will call the function with this signature (if the match is a multi-key
21 * combination the callback will still be called only once): (String key, Ext.EventObject e)
22 * A KeyMap can also handle a string representation of keys.<br />
25 // map one key by key code
26 var map = new Ext.KeyMap("my-element", {
27 key: 13, // or Ext.EventObject.ENTER
32 // map multiple keys to one action by string
33 var map = new Ext.KeyMap("my-element", {
39 // map multiple keys to multiple actions by strings and array of codes
40 var map = new Ext.KeyMap("my-element", [
43 fn: function(){ alert("Return was pressed"); }
46 fn: function(){ alert('a, b or c was pressed'); }
51 fn: function(){ alert('Control + shift + tab was pressed.'); }
55 * <b>Note: A KeyMap starts enabled</b>
57 * @param {Mixed} el The element to bind to
58 * @param {Object} config The config (see {@link #addBinding})
59 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
61 Ext.KeyMap = function(el, config, eventName){
62 this.el = Ext.get(el);
63 this.eventName = eventName || "keydown";
66 this.addBinding(config);
71 Ext.KeyMap.prototype = {
72 <div id="prop-Ext.KeyMap-stopEvent"></div>/**
73 * True to stop the event from bubbling and prevent the default browser action if the
74 * key was handled by the KeyMap (defaults to false)
79 <div id="method-Ext.KeyMap-addBinding"></div>/**
80 * Add a new binding to this KeyMap. The following config object properties are supported:
82 Property Type Description
83 ---------- --------------- ----------------------------------------------------------------------
84 key String/Array A single keycode or an array of keycodes to handle
85 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)
86 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)
87 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)
88 handler Function The function to call when KeyMap finds the expected key combination
89 fn Function Alias of handler (for backwards-compatibility)
90 scope Object The scope of the callback function
91 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)
97 var map = new Ext.KeyMap(document, {
98 key: Ext.EventObject.ENTER,
103 //Add a new binding to the existing KeyMap later
111 * @param {Object/Array} config A single KeyMap config or an array of configs
113 addBinding : function(config){
114 if(Ext.isArray(config)){
115 Ext.each(config, function(c){
120 var keyCode = config.key,
121 fn = config.fn || config.handler,
122 scope = config.scope;
124 if (config.stopEvent) {
125 this.stopEvent = config.stopEvent;
128 if(typeof keyCode == "string"){
130 var keyString = keyCode.toUpperCase();
131 for(var j = 0, len = keyString.length; j < len; j++){
132 ks.push(keyString.charCodeAt(j));
136 var keyArray = Ext.isArray(keyCode);
138 var handler = function(e){
139 if(this.checkModifiers(config, e)){
142 for(var i = 0, len = keyCode.length; i < len; i++){
147 fn.call(scope || window, k, e);
156 fn.call(scope || window, k, e);
161 this.bindings.push(handler);
165 checkModifiers: function(config, e){
166 var val, key, keys = ['shift', 'ctrl', 'alt'];
167 for (var i = 0, len = keys.length; i < len; ++i){
170 if(!(val === undefined || (val === e[key + 'Key']))){
177 <div id="method-Ext.KeyMap-on"></div>/**
178 * Shorthand for adding a single key listener
179 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
181 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
182 * @param {Function} fn The function to call
183 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
185 on : function(key, fn, scope){
186 var keyCode, shift, ctrl, alt;
187 if(typeof key == "object" && !Ext.isArray(key)){
206 handleKeyDown : function(e){
207 if(this.enabled){ //just in case
208 var b = this.bindings;
209 for(var i = 0, len = b.length; i < len; i++){
215 <div id="method-Ext.KeyMap-isEnabled"></div>/**
216 * Returns true if this KeyMap is enabled
219 isEnabled : function(){
223 <div id="method-Ext.KeyMap-enable"></div>/**
224 * Enables this KeyMap
228 this.el.on(this.eventName, this.handleKeyDown, this);
233 <div id="method-Ext.KeyMap-disable"></div>/**
234 * Disable this KeyMap
238 this.el.removeListener(this.eventName, this.handleKeyDown, this);
239 this.enabled = false;
243 <div id="method-Ext.KeyMap-setDisabled"></div>/**
244 * Convenience function for setting disabled/enabled by boolean.
245 * @param {Boolean} disabled
247 setDisabled : function(disabled){
248 this[disabled ? "disable" : "enable"]();