3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.KeyMap"></div>/**
\r
11 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
\r
12 * The constructor accepts the same config object as defined by {@link #addBinding}.
\r
13 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
\r
14 * combination it will call the function with this signature (if the match is a multi-key
\r
15 * combination the callback will still be called only once): (String key, Ext.EventObject e)
\r
16 * A KeyMap can also handle a string representation of keys.<br />
\r
19 // map one key by key code
\r
20 var map = new Ext.KeyMap("my-element", {
\r
21 key: 13, // or Ext.EventObject.ENTER
\r
26 // map multiple keys to one action by string
\r
27 var map = new Ext.KeyMap("my-element", {
\r
33 // map multiple keys to multiple actions by strings and array of codes
\r
34 var map = new Ext.KeyMap("my-element", [
\r
37 fn: function(){ alert("Return was pressed"); }
\r
40 fn: function(){ alert('a, b or c was pressed'); }
\r
45 fn: function(){ alert('Control + shift + tab was pressed.'); }
\r
49 * <b>Note: A KeyMap starts enabled</b>
\r
51 * @param {Mixed} el The element to bind to
\r
52 * @param {Object} config The config (see {@link #addBinding})
\r
53 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
\r
55 Ext.KeyMap = function(el, config, eventName){
\r
56 this.el = Ext.get(el);
\r
57 this.eventName = eventName || "keydown";
\r
60 this.addBinding(config);
\r
65 Ext.KeyMap.prototype = {
\r
66 <div id="prop-Ext.KeyMap-stopEvent"></div>/**
\r
67 * True to stop the event from bubbling and prevent the default browser action if the
\r
68 * key was handled by the KeyMap (defaults to false)
\r
73 <div id="method-Ext.KeyMap-addBinding"></div>/**
\r
74 * Add a new binding to this KeyMap. The following config object properties are supported:
\r
76 Property Type Description
\r
77 ---------- --------------- ----------------------------------------------------------------------
\r
78 key String/Array A single keycode or an array of keycodes to handle
\r
79 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
80 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
81 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
82 handler Function The function to call when KeyMap finds the expected key combination
\r
83 fn Function Alias of handler (for backwards-compatibility)
\r
84 scope Object The scope of the callback function
\r
85 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 var map = new Ext.KeyMap(document, {
\r
92 key: Ext.EventObject.ENTER,
\r
97 //Add a new binding to the existing KeyMap later
\r
105 * @param {Object/Array} config A single KeyMap config or an array of configs
\r
107 addBinding : function(config){
\r
108 if(Ext.isArray(config)){
\r
109 Ext.each(config, function(c){
\r
110 this.addBinding(c);
\r
114 var keyCode = config.key,
\r
115 fn = config.fn || config.handler,
\r
116 scope = config.scope;
\r
118 if (config.stopEvent) {
\r
119 this.stopEvent = config.stopEvent;
\r
122 if(typeof keyCode == "string"){
\r
124 var keyString = keyCode.toUpperCase();
\r
125 for(var j = 0, len = keyString.length; j < len; j++){
\r
126 ks.push(keyString.charCodeAt(j));
\r
130 var keyArray = Ext.isArray(keyCode);
\r
132 var handler = function(e){
\r
133 if(this.checkModifiers(config, e)){
\r
134 var k = e.getKey();
\r
136 for(var i = 0, len = keyCode.length; i < len; i++){
\r
137 if(keyCode[i] == k){
\r
138 if(this.stopEvent){
\r
141 fn.call(scope || window, k, e);
\r
147 if(this.stopEvent){
\r
150 fn.call(scope || window, k, e);
\r
155 this.bindings.push(handler);
\r
159 checkModifiers: function(config, e){
\r
160 var val, key, keys = ['shift', 'ctrl', 'alt'];
\r
161 for (var i = 0, len = keys.length; i < len; ++i){
\r
164 if(!(val === undefined || (val === e[key + 'Key']))){
\r
171 <div id="method-Ext.KeyMap-on"></div>/**
\r
172 * Shorthand for adding a single key listener
\r
173 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
\r
174 * following options:
\r
175 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
\r
176 * @param {Function} fn The function to call
\r
177 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
\r
179 on : function(key, fn, scope){
\r
180 var keyCode, shift, ctrl, alt;
\r
181 if(typeof key == "object" && !Ext.isArray(key)){
\r
200 handleKeyDown : function(e){
\r
201 if(this.enabled){ //just in case
\r
202 var b = this.bindings;
\r
203 for(var i = 0, len = b.length; i < len; i++){
\r
204 b[i].call(this, e);
\r
209 <div id="method-Ext.KeyMap-isEnabled"></div>/**
\r
210 * Returns true if this KeyMap is enabled
\r
211 * @return {Boolean}
\r
213 isEnabled : function(){
\r
214 return this.enabled;
\r
217 <div id="method-Ext.KeyMap-enable"></div>/**
\r
218 * Enables this KeyMap
\r
220 enable: function(){
\r
222 this.el.on(this.eventName, this.handleKeyDown, this);
\r
223 this.enabled = true;
\r
227 <div id="method-Ext.KeyMap-disable"></div>/**
\r
228 * Disable this KeyMap
\r
230 disable: function(){
\r
232 this.el.removeListener(this.eventName, this.handleKeyDown, this);
\r
233 this.enabled = false;
\r
237 <div id="method-Ext.KeyMap-setDisabled"></div>/**
\r
238 * Convenience function for setting disabled/enabled by boolean.
\r
239 * @param {Boolean} disabled
\r
241 setDisabled : function(disabled){
\r
242 this[disabled ? "disable" : "enable"]();
\r